简体   繁体   中英

how to get the status of file upload in ajax post

How the get the status of uploaded files when user clicks on cancel while uploading multiple files using ajax call.

This way im calling the ajax request to upload files:

 var request = $.ajax({
     url: 'files.php',
     type: 'POST',
     data: data,
     cache: false,
     contentType: false,
     processData: false,
     beforeSend: function () {
         $(".progressbar").show();
     },
     xhr: function () {
         var xhr = $.ajaxSettings.xhr();
         if (xhr.upload) {
             xhr.upload.addEventListener('progress', showProgress, false);
         }
         return xhr;
     },
     success: function (data) {
         if (percentComplete <= 100) {
             $('#pb div').animate({
                 width: '100%'
             }, {
                 step: function (now) {
                     $(this).text(Math.round(now) + '%');
                 },
                 duration: 10
             });
         }
         $('#uplcomp').append(data);
     }
 });

If user clicks on cancel button im doing this:

request.abort();

As the above statement just abort the ajax request , im not getting any response like how many files are uploaded, how much mb uploaded etc....

Can anyone help me on this?

I don't think that there is default way for your question. The information you're looking for - how many files are uploaded - is not stored in the XMLHttpRequest object by my knowlegde.

I suppose (but never tried it) that you can create somehting custom for it. High level analysis: keep track of your upload progress server side. Store some info in your database regarding the upload: amount of files to process, amount of files processed, date/time, unique upload/session ID. When you abort the the upload, or it fails you can retrieve the information, based on a unique ID, about the upload process via ajax from your DB and present it in the UI.

Note: If the request has already been sent to the server then the server will process the request even if we abort the request but the client will not wait for/handle the response.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM