. What we’ve done recently is refactoring existing projects. Change WEB FROM to MVC, and you’re starting all over again.

There is an import function, upload files. The original idea was to have a hidden iframe, and in the page of this iframe, set a form with a file upload control. On the server side, after receiving the uploaded file, it actually saves the file and then reads it.

What a strange way to do it. Read in the contents of the user file and then use it without actually saving the file first, because it is a temporary file and has no value. You can read the contents of uploaded files directly and discard them immediately.

Second, I understand why a hidden IFrame was used in the first place. Because uploading a file requires submission, and submitting a page will cause a refresh, in order to avoid page content refresh, we created a hidden iframe and let it take charge of submission. Good is good, just want to make one more page.

In a new project, ajax is responsible for submitting the front-end files; Server side, after receiving files directly processing, do not save, use up immediately discard:

Front end:

<! -- Hidden upload control, --> <input type="file" id="fileImportData" name="fileImportData" onchange=" f_pointxy.importData ()" style="display:none;" / > <! -- Import button, click it will trigger the click event of upload control, <input type="button" value=" import "class=" BTN mini minilt" onclick='javascript:document.getElementById("fileImportData").click(); '/ >Copy the code
<script type="text/javascript">
    var f_pointXY = function () {

        function importData() {$("#formImport").ajaxSubmit({
                url: "@Url.StaticFile("~/Common/YongHai/importData/")" + $("#txt_SMID").val(),
                type: 'post'.data: {},
                success: function (data) {
                    if (data.substr(0.2) = ="OK") {
                        alert("Import successful");
                    } else{ alert(data); }},error: function (e) { alert(e); }}); }return {
            importData: function () {
                importData();
            }
        };

    }();
</script>
Copy the code

Server side:

        [HttpPost]
        public ActionResult importData(int id)
        {
            if (Request.Files.Count == 0)
            {
                return Content("Failed to receive uploaded data"."text/html");
            }

            string msg = "OK";

            HttpPostedFileBase fb = Request.Files[0];
            string[] Acs_FilePath = fb.FileName.Split('\ \');
            string Acs_FileName = Acs_FilePath[Acs_FilePath.Length - 1];
            string Acs_FileType = Acs_FileName.Split('. ') [1].ToLower();
            string xyType = Request["xyType"]????"";

            using (Stream fs = fb.InputStream)
            {
                    int fileLen = fb.ContentLength;
                    byte[] input = new byte[fileLen];
                    fs.Read(input, 0, fileLen);
                    string str = System.Text.Encoding.UTF8.GetString(input);
                    msg += str;
            }

            END:
            return Content(msg, "text/html");
        }
Copy the code

In this way, click the import button, you can upload and import.

Monkey sai.