Abort a multiple file upload AJAX request

自作多情 提交于 2019-12-20 14:35:11

问题


I am trying to abort a multiple file upload with a progress bar, showing the state of the process.

What I want to achieve is to completely abort the multiple file upload on the abort button click; to stop the progress bar as well as to clear every file(s) that might have been uploaded during the course of the initially triggered multiple file upload process.

Below is my code:

var AJAX = $.ajax({ 
    xhr: function() {
        var XHR = new window.XMLHttpRequest();

        XHR.upload.addEventListener('progress', function(e) {
          if (e.lengthComputable) {

            var PROGRESS = Math.round((e.loaded/e.total)*100);
            $('#PROGRESS_BAR').text(PROGRESS);

        } }, false); return XHR; },
    url        : '/php.php',
    type       : 'POST',
    data       : DATA,
    cache      : false,
    processData: false,
    contentType: false,
    beforeSend : function() { }, 
    success    : function() { } 
});

$(document).on('click', '.ABORT', function(e) {     
    AJAX.abort();
});

I use the code above to dynamically upload images with a progress bar.

I found lots of articles using .abort() to stop the process, but that seemed to only work browser side and not server side.

In what way can I cease the upload completely as in: on both client and server side as .abort() does not enable me get the desirable result?


回答1:


Try this:

var XHR = new window.XMLHttpRequest();
var AJAX = $.ajax({ 
    xhr: function() {
        XHR.upload.addEventListener('progress', function(e) {
          if (e.lengthComputable) {

            var PROGRESS = Math.round((e.loaded/e.total)*100);
            $('#PROGRESS_BAR').text(PROGRESS);

        } }, false); return XHR; },
    url        : '/php.php',
    type       : 'POST',
    data       : DATA,
    cache      : false,
    processData: false,
    contentType: false,
    beforeSend : function() { }, 
    success    : function() { } 
});

$(document).on('click', '.ABORT', function(e){      
    XHR.abort();
});


来源:https://stackoverflow.com/questions/27749521/abort-a-multiple-file-upload-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!