Home  >  Article  >  Web Front-end  >  JQuery plug-in ajaxfileupload.js asynchronous file upload example_jquery

JQuery plug-in ajaxfileupload.js asynchronous file upload example_jquery

WBOY
WBOYOriginal
2016-05-16 15:58:271114browse

During the file upload process on the server side, if you use the short-end upload control of the web server to upload files, the page will be refreshed once, which is not very friendly to the user experience. ajaxfileupload.js is a jQuery asynchronous file upload plug-in that is simple to use and easy to use.

Prerequisite: ajaxfileupload.js file, just download one from Baidu.

JS reference:

Copy code The code is as follows:



html code:

Copy code The code is as follows:



JS code:
Copy code The code is as follows:

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',//Upload control ID
                                  //dataType: 'json',
                   error: function () { alert('error'); },
Success: function (datax) {
If (datax != "") {
                                 msgShow('System prompt', 'Upload successful!', 'info');
                            } else {
                                  msgShow('System prompt', 'Upload failed!', 'info');
                                                                                                       }                  }
                });
              } else {
$ .Messager.alert ('prompt', 'Please select the upload file', 'info');
            }
}


Backend code:

Copy code The code is as follows:

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;
//Specify the saving path of the uploaded file on the server
                    string savePath = context.Server.MapPath("~/upload/");
//Check whether this physical path exists on the server, if not, create it
If (!System.IO.Directory.Exists(savePath))
                                                {
System.IO.Directory.CreateDirectory(savePath);
                }
                     savePath = savePath name;//Upload file path
                files[0].SaveAs(savePath);//Save the file
                       context.Response.Write(savePath);
            }
              catch (Exception ex)
                {
                    context.Response.Write("FileUpload: " ex.Message);
            }

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn