0

I have this html

<input type="file" name="[]" multiple />

Now I want to upload each of the files separately but I am really stuck.

$('input:file').on('change', function(){
    allFiles = $(this)[0].files;
    for(var i = 0; allFiles.length > i; i++){
        eachFile = allFiles[i];
        $.ajax({
            url: link,
            type: "POST",
            data: new FormData(eachFile),
            contentType: false,
            processData:false,
            success: function(result){
                console.log(result);
            }
        })
    }
})

But I don't seem to work. I use the above ajax request to upload files in an array but I want to upload them differently. Any suggestion on how to achieve this?

This is not a duplicate to the supposed duplicate question. I want to upload multiple files separately to the server but the question marked wants to upload multiple files at once. I use my above code to upload multiple files to the server at once an it works fine. So why should I as what I already have a solution to?

5
  • why would you want to upload multiple files separately? Commented Dec 17, 2016 at 16:27
  • maybe what you are asking is handling each files separately on the server Commented Dec 17, 2016 at 16:27
  • maybe use - or get inspiration from - blueimp.github.io/jQuery-File-Upload Commented Dec 17, 2016 at 16:28
  • Possible duplicate of How to upload multiple files using PHP, jQuery and AJAX Commented Dec 17, 2016 at 16:31
  • I don't see how this is a duplicate. I want the upload the files separately but the marked duplicate question want to upload multiple files at once. Commented Dec 17, 2016 at 16:39

1 Answer 1

2

Use this... Tested (in chrome) and working

$('input:file').on('change', function(){
    allFiles = $(this)[0].files;
    for(var i = 0; allFiles.length > i; i++){
        var eachFile = allFiles[i],
        fileData = new FormData();
        fileData.append('file', eachFile);
        $.ajax({
            url: link,
            type: "POST",
            datatype:'script',
            data: fileData,
            contentType: false,
            processData:false,
            success: function(result){
                console.log(result);
            }
        })
    }
})
Sign up to request clarification or add additional context in comments.

Comments

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.