使用 jquery ajax 和 FormData 跟踪文件上传的 ajax 发布进度 [英] Track ajax post progress for fileupload using jquery ajax and FormData

查看:19
本文介绍了使用 jquery ajax 和 FormData 跟踪文件上传的 ajax 发布进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var files = [];

$(document).ready(function (){
    dropArea = document.getElementById("droparea");
});

// when we drag and drop files into the div#droparea
dropArea.addEventListener("drop", function (evt) {
    files = evt.dataTransfer.files;
}, false);  

function uploadFiles(stepX) {
    var url = "/ajax/uploadfiles.php";
    var type = "POST";

    if (files.length > 0) {
         var data = new FormData(); // we use FormData here to send the multiple files data for upload

          for (var i=0; i<files.length; i++) {
       var file = files[i];
             data.append(i, file);  
   }
         //start the ajax
         return $.ajax({
   //this is the php file that processes the data and send mail
         url: url,  

  //POST method is used
  type: type,

   //pass the data          
   data: data,      

    //Do not cache the page
          cache: false,

// DO NOT set the contentType and processData
// see http://stackoverflow.com/a/5976031/80353
contentType: false,
processData: false,

//success
      success: function (json) {                
      //if POST is a success expect no errors
     if (json.error == null && json.result != null) {                   
                    data = json.result.data;

                // error 
                } else {
                    alert(json.error);  
                }           
            }       
        });
    } 
    return {'error' : 'No files', 'result' : null};
}

如果我想保留这种上传文件到服务器的方法,如何跟踪文件上传的进度?

How do I track the progress of the file upload if I want to retain this method to upload files to server?

推荐答案

注意@TODO 的注释

Note the comment with @TODO

//start the ajax
return $.ajax({
            //this is the php file that processes the data and send mail
            url: url,   

            //POST method is used
            type: type,

            //pass the data         
            data: data,     

            //Do not cache the page
            cache: false,

            //@TODO start here
            xhr: function() {  // custom xhr
                myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){ // check if upload property exists
                    myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
                }
                return myXhr;
            },
            //@TODO end here

            // DO NOT set the contentType and processData
            // see http://stackoverflow.com/a/5976031/80353
            contentType: false,
            processData: false,

添加一个更新进度的独立函数.

Add a standalone function that updates the progress.

function updateProgress(evt) {
    console.log('updateProgress');
    if (evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            console.log(percentComplete);
    } else {
            // Unable to compute progress information since the total size is unknown
            console.log('unable to complete');
    }
}

来自 https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest > 监控进度

这篇关于使用 jquery ajax 和 FormData 跟踪文件上传的 ajax 发布进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆