JQuery外掛程式ajaxfileupload.js非同步上傳檔案執行個體,jqueryajax非同步

來源:互聯網
上載者:User

JQuery外掛程式ajaxfileupload.js非同步上傳檔案執行個體,jqueryajax非同步

在伺服器端做檔案上傳的過程中,如果使用web伺服器短端的上傳控制項去上傳檔案的話,會導致頁面重新整理一次,這樣對使用者的體驗就不是很友好了。ajaxfileupload.js是一款jQuery的非同步上傳檔案外掛程式,使用簡單且容易上手。

前置條件:ajaxfileupload.js檔案,百度下載一個就行。

JS引用:
複製代碼 代碼如下:
<script src="/Content/JQueryJS/jquery-2.1.1.js"></script>
<script src="/Content/Js/ajaxfileupload.js"></script>

html代碼:
複製代碼 代碼如下:
 <input id="fileToUpload" type="file" name="fileToUpload">

JS代碼:
複製代碼 代碼如下:
function saveCInfo() {
            var filename = document.getElementById("fileToUpload").value;
            if (filename != "") {
                $.ajaxFileUpload({
                    url: '../Order/OrderExec.ashx?oprMode=fileUpload' + "&filename=" + filename + "&billno=" + billno + "&companyname=" + companyname,
                    secureuri: false,
                    fileElementId: 'fileToUpload',//上傳控制項ID
                    //dataType: 'json',
                    error: function () { alert('error'); },
                    success: function (datax) {
                        if (datax != "") {
                            msgShow('系統提示', '上傳成功!', 'info');
                        } else {
                            msgShow('系統提示', '上傳失敗!', 'info');
                        }
                    }
                });
            } else {
                $.messager.alert('提示', '請選擇上傳檔案', 'info');
            }
        }

後台代碼:

複製代碼 代碼如下:
public void FileUpload(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/html";
                string companyname = context.Request.Params["companyname"];
                string billno = context.Request.Params["billno"];
                string filename = context.Request.Params["filename"];
                string name = companyname + "_" + billno + "_" + filename;
                HttpFileCollection files = HttpContext.Current.Request.Files;
                //指定上傳檔案在伺服器上的儲存路徑
                string savePath = context.Server.MapPath("~/upload/");
                //檢查伺服器上是否存在這個實體路徑,如果不存在則建立
                if (!System.IO.Directory.Exists(savePath))
                {
                    System.IO.Directory.CreateDirectory(savePath);
                }
                savePath = savePath + name;//上傳檔案路徑
                files[0].SaveAs(savePath);//儲存檔案
                context.Response.Write(savePath);
            }
            catch (Exception ex)
            {
                context.Response.Write("FileUpload: " + ex.Message);
            }

        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.