0

i use ajax to upload multiple file.user can upload multiple file in the same time.how i can abort ajax only for one uploading file? this is ajax :

areq=$.ajax({
                url: $(this).attr('action'),
                xhr: function() { // custom xhr (is the best)
                    var xhr = new XMLHttpRequest();
                    //load
                    xhr.upload.addEventListener("load", function(evt) {
                        $("#msg").text('success !' );
                    }, false);
                    //error
                    xhr.upload.addEventListener("error", function(evt) {
                        // show progress like example
                        $("#msg").text('error !' );
                    }, false);
                    //abort
                    xhr.upload.addEventListener("abort", function(evt) {
                        $("#msg").text('abort !' );
                    }, false);
                    return xhr;
                },
            type: 'post',
            processData: false,
            contentType: false,
            data: fd,
            success: function(data) {
               // do something... 
            }
        });
adib16
  • 1,649
  • 2
  • 20
  • 35

1 Answers1

-1

maximum number for upload file is 10.so i define this variable at the first of js file: var sssss,formaa,formab,formac,formad,formae,formaf,formag,formah,formai,formaj; and save $.ajax to sssss .after $.ajax put this switch :

stra=$(this).parent().index();
        switch (stra) {
            case 10:
                formaa = sssss;
                break;
            case 9:
                formab = sssss;
                break;
            case 8:
                formac = sssss;
                break;
            case 7:
                formad = sssss;
                break;
            case 6:
                formae = sssss;
                break;
            case 5:
                formaf = sssss;
                break;
            case 4:
                formag = sssss;
                break;
            case 3:
                formah = sssss ;
                break;
            case 2:
                formai = sssss;
                break;
            case 1:
                formaj = sssss;
                break;
        }

stra is number of file that is uploading.when user click on cancel button this function called:

$("form span#stop").click(function(){
            stra=$(this).parent().parent().index();
            switch (stra) {
            case 10:
                formaa.abort() ;
                break;
            case 9:
                formab.abort();
                break;
            case 8:
                formac.abort();
                break;
            case 7:
                formad.abort();
                break;
            case 6:
                formae.abort();
                break;
            case 5:
                formaf.abort();
                break;
            case 4:
                formag.abort();
                break;
            case 3:
                formah.abort();
                break;
            case 2:
                formai.abort();
                break;
            case 1:
                formaj.abort();
                break;
            }
        });

stra is also number of uploading file .so i copy $.ajax to another variable and that variable consider for every file.

adib16
  • 1,649
  • 2
  • 20
  • 35
  • so you _are_ initiation a separate `$.ajax()` call for each file? Didn't you ever hear of arrays? – Alnitak Jan 11 '15 at 17:11