1
<script>
funcupload(i) {
var formData = new FormData();
files = $("#upload").get(0).files;

formData.append("upfiles[]", files[i]);

$.ajax({
    url:"upload.php",
    type: 'post',
    data: formData,
    cache: false,
    processData: false,
    contentType: false,
    success:function(data) {
alert("Complete");
i++;
}
}
</script>

<input type=\"file\" id=\"upload\" name=\"upfiles[]\" multiple>
<input type=\"button\" value=\"Upload\" name=\"upload\" onclick=\"funcupload(0)\">

I want to queue for upload files. In my above code when first file is uploading, second file is pending status. But if upload button clicked again after select files then first file is starting to upload same time with first file added to the previous click of upload button.

How can queue ajax upload for files Or in other words how can added next files to current ajax?

1 Answer 1

2

What I think you want to do:

  • user select 4 files to upload.
  • User clicks button
  • you want to do an ajax-call for each file, but they can't overlap

If that's the case, then you can do this:

var filesToUpload = null;

var uploadNextFile(counter)
{
    $.ajax({
        url:"upload.php",
        type: 'post',
        data: files[i],
        cache: false,
        processData: false,
        contentType: false,
        success:function(data) {
            if (counter < filesToUpload.length)
               uploadNextFile(counter++);
            else
               filesToUpload = null;

        }
    }
}

Now, when the submit-button is clicked, you call uploadNextfile() and pass the files and counter 0.

If the user adds files, you can check if filesToUpload is empty or not. If it's not empty, you can add the files, and the code will continue running:

filesToUpload.push(/*extra files*/);

If it's empty, you can just call the function again.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. but if user selected another 4 files when pervious 4 files is uploading then new files overlap on pervious 4 files to upload!
@kelly I've changed my code to reflect your problem.
Disable the "choose files" button while these 4 files are still being uploaded.. Show progress bar in the mean time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.