Used before the ajaxfileupload, but the feeling is not very easy to use, today, the front-end file on the issue of a point, simply a lunch break on their own extended a simple jQuery ajax upload file plug-ins, need to use the upload function of the page, just in the jQuery introduction, the introduction of the JS file can be. Due to the use of Formdata, the lower version of IE does not necessarily support, we are backstage management system, are used Chrome,firefox and 360 of the domestic Chrome core browser, so do not consider this issue.
Sticky code to remember: Fileupload.js
/** * jquery upload file extension * Use Method Example (optional parameters in square brackets): * $.fileupload ({* URL: ...,//File upload address *: "#uploadFile", The CSS selector Specifies the file tags to be uploaded * [filename: ...],//uploaded to the server's filename * [data:{id:1,user: "Xiaoming"}],//Custom uploaded parameters * [su
Ccess:function (data) {Console.log (data)}]////Post successful callback function *}); * Note: This plugin IE9 and below does not support * * AUTHOR:LIXK * date:2016/11/24/var fileupload = function (params) {var file = $ (param s["File"]). Get (0) | | {}; File object var filename = params["filename"] | | File.name; File name for upload to server, default file name var filecontent = File.files[0] | | {}; File content var url = params["url"]; Upload address var data = params["Data"] | | {}; Custom parameter var success = params["Success"]; Upload successful callback function var formData = new FormData ();
Create Upload Form formdata.append (fileName, filecontent);
Add parameters to the form for (var key in data) {Formdata.append (key, Data[key]); ///Send form data to server $.ajax ({url:url, type: "POST", Data:formdata, Processdata:false,//tell jquery not to handle sending data con Tenttype:False,//tell jquery not to set Content-type request header success:function (data) {//request succeeded, callback if (success) {success (data);
}
}
}); }//Add JQuery extension jquery.extend ({fileupload:fileupload});
Picture Upload Use Example:
$.fileupload ({
url:uploadlogo_url,
file: ' Input[name= ' logofile "] ',
//filename:" Logofile ",
Success:function (data) {
$ ("#logoImg"). attr ("src", data.imgpath);//preview Picture
}
});