Give Layui upload a progress bar with each file,. NET Background code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.