1. upload.js extension
Function using Ajax's XHR property implementation
This feature modifies the Upload.js file in modules
Features specific implementations:
Adding a listener function to the JS file
//Creating a listener function varxhronprogress=function (fun) {xhronprogress.onprogress= fun;//Binding Monitoring//using closures to implement listener bindings returnfunction () {//get XMLHttpRequest objects by $.AJAXSETTINGS.XHR (); varXHR =$.ajaxsettings.xhr (); //determine if the listener function is a function if(typeofXhronprogress.onprogress!=='function') returnXHR; //If there is a listener function and the XHR object supports binding, the listener function is bound if(Xhronprogress.onprogress &&xhr.upload) {xhr.upload.onprogress=xhronprogress.onprogress; } returnXHR; } }
Initializing uploads
//initializing uploadsUpload.render ({elem:'Upload Address', Url:path+'/upload/uploadvideo.do', Accept:'Video', Size:512000, Auto:false, Xhr:xhronprogress, progress:function (value) {//Upload Progress Callback value progress valueElement.progress ('Demo', value+'%')//Set page progress bar}, Bindaction:'#uploadvideo', before:function (input) {//returns the parameter item, which is the current input DOM objectConsole.log ('the file crosses'); }, Done:function (res) {//Upload SuccessfulConsole.log (RES)}});
To modify the Ajax method for upload.js files in modules
//Submit File$.ajax ({url:options.url, Type:options.method, Data:formdata, Contentty PE:false, ProcessData:false, DataType:'JSON', XHR:OPTIONS.XHR (function (e) {//new features are added here varPercent=math.floor ((e.loaded/e.total) * -);//Calculate PercentageOptions.progress (percent);//callback returns the value}), Success:function (res) {Successful++; Done (index, RES); Alldone (); }, Error:function (e) {Console.log (e) aborted++; That.msg ('An exception occurred on the request upload interface'); Error (index); Alldone (); } });
Background code:
Publicactionresult uploadfiles (httppostedfilebase fileNames) {stringPath =""; //less than 20M if(Filenames.contentlength >0&& Filenames.contentlength <=120971520) { varFileName =Path.getfilename (filenames.filename); stringQ_FN = filename.substring (0, Filename.lastindexof (".")); stringH_FN = filename.substring (Filename.lastindexof (".")); stringNewFileName = Q_fn + DateTime.Now.ToString ("YYYYMMDDHHMMSS") +H_FN; Path= Path.Combine (Server.MapPath ("/uploadfile/"), NewFileName); Filenames.saveas (path); Path="/uploadfile/"+NewFileName; varRelt =New{TC =path}; returnContent (Jsonconvert.serializeobject (Relt)); } Else { varRelt =New{TC ="upload file less than 20M" }; returnContent (Jsonconvert.serializeobject (Relt)); } }
function to this end!!!
Liezi:
Reference: http://fly.layui.com/jie/19430/
Give Layui upload a progress bar with each file,. NET Background code