afm7
afm7

Reputation: 23

Image Uploading with FormData multiple inputs

How I can use FormData with multiple file inputs?

<input type="file" id="file1">

<input type="file" id="file2">

<input type="file" id="file3">

Upvotes: 0

Views: 49

Answers (1)

Jack jdeoel
Jack jdeoel

Reputation: 4584

Now html5 support multiple file upload in one input filed ! If you want to start upload auto without click , you should do by onchange method . From ajax , you must use new FormData() .

upload.php

<input type="file" id="files" name="filefield" multiple="multiple">
<script type="text/javascript">
$("#files").on("change",function(){
    var ajaxData = new FormData();
    var obj = $(this)[0];
    $.each(obj.files,function(i,file){
        ajaxData.append("file['"+i+"']",file);
    });
    $.ajax({
        url :'index.php',
        data: ajaxData,
        contentType: false,
        processData: false,
        dataType: 'json',
        type:"POST",
        success : function() {

        }
    });
})

index.php

<?php
  var_dump($_FILES);
?>

Upvotes: 1

Related Questions