最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Abort a multiple file upload AJAX request - Stack Overflow

programmeradmin7浏览0评论

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 pletely 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 pletely as in: on both client and server side as .abort() does not enable me get the desirable result?

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 pletely 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 pletely as in: on both client and server side as .abort() does not enable me get the desirable result?

Share Improve this question edited Jan 17, 2017 at 11:27 nyedidikeke 7,6688 gold badges47 silver badges63 bronze badges asked Jan 2, 2015 at 22:44 user3565264user3565264 1572 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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();
});
发布评论

评论列表(0)

  1. 暂无评论