Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

javascript - Track ajax post progress for fileupload using jquery ajax and FormData

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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');
    }
}

From https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest > Monitoring progress


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...