js文件上传进度

2018-12-12  本文已影响0人  FMaarten
$.ajax({
    url: “******”,
    cache: false,
    contentType:"multipart/form-data",
    dataType: "json",
    type: "POST",
    contentType: false,
    processData: false,
    data: formData,
    xhr:function(){
            myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){// check if upload property exists
            myXhr.upload.addEventListener('progress',function (e) {
            // event.total是需要传输的总字节,event.loaded是已经传输的字节。如果event.lengthComputable不为真,则event.total等于0
                    if (e.lengthComputable) {
                        var progress =  Math.round(e.loaded / e.total * 100)+ "%";//已上传的进度
                }
               },false);// for handling the progress of the upload
            }
            return myXhr;
        },
        success: function (result) {
        },
        error: function (e) {
        }
})
上一篇 下一篇

猜你喜欢

热点阅读