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?

doggie brezy
  • 289
  • 3
  • 16
  • why would you want to upload multiple files separately? – ffff Dec 17 '16 at 16:27
  • maybe what you are asking is handling each files separately on the server – ffff Dec 17 '16 at 16:27
  • maybe use - or get inspiration from - https://blueimp.github.io/jQuery-File-Upload/ – flowtron Dec 17 '16 at 16:28
  • Possible duplicate of [How to upload multiple files using PHP, jQuery and AJAX](http://stackoverflow.com/questions/19295746/how-to-upload-multiple-files-using-php-jquery-and-ajax) – ffff Dec 17 '16 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. – doggie brezy Dec 17 '16 at 16:39

1 Answers1

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);
            }
        })
    }
})
KANAYO AUGUSTIN UG
  • 2,078
  • 3
  • 17
  • 31