asp.net mvc + javascript匯入檔案內容

來源:互聯網
上載者:User

。近期做的是對現有項目進行重構。WEB FROM改成MVC,其實也算是推倒重來了。

裡面有一個匯入功能,將檔案上傳。原先的做法是有一個隱藏的iframe,在這個iframe的頁面中設定一個表單form,form中有一個檔案上傳控制項。而在伺服器端,接收到上傳檔案後,真的先將檔案儲存,然後讀取之。

好奇怪的做法啊。將使用者檔案內容讀入,然後利用之,不必真的將檔案先儲存下來的,因為這是臨時檔案,沒有儲存的價值。完全可以將上傳的檔案內容直接讀出,用完即棄。

其次,我理解原先為何要用一個隱藏的iframe。因為上傳檔案需要提交,而提交頁面會導致重新整理,為了避免頁面內容重新整理,於是搞了個隱藏的iframe,讓它來負責提交。好是好,就是要多搞一個頁面。

在新項目裡面,前端,檔案由ajax負責提交;伺服器端,接收檔案後直接處理,不儲存下來,用完即棄:

前端:

<!-- 隱藏的上傳控制項,onchange時就觸發提交 --><input type="file" id="fileImportData" name="fileImportData" onchange="f_pointXY.importData()" style="display:none;" />  <!-- 匯入按鈕,點擊後就觸發上傳控制項的點擊事件,彈出檔案選擇框 --><input type="button" value="匯入" class="btn mini minilt" onclick='javascript:document.getElementById("fileImportData").click();' />
<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("匯入成功");                    } else {                        alert(data);                    }                },                error: function (e) {                    alert(e);                }            });        }        return {            importData: function () {                importData();            }        };    }();</script>

伺服器端:

        [HttpPost]        public ActionResult importData(int id)        {            if (Request.Files.Count == 0)            {                return Content("接收上傳資料失敗", "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");        }

這樣,點擊匯入按鈕,即可上傳並匯入矣。

猴賽雷。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.