0

I have below upload function that uploads one image at a time, how do I adapt it to handle multiple files ?

function _(el){
    return document.getElementById(el);
}
function uploadFile(){
    var checked_box = $('input:checkbox:checked').val();
    var file = _("image").files[0];
    var imageFile =$("#image").val();
    var formdata = new FormData();
    formdata.append( 'action','add_photo');
    formdata.append("image", file);
    jQuery.each($("input[name^='image']")[0].files, function(i, file) {
    formdata.append('photo['+i+']', file);
    }); 
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "includes/add_photo.inc.php?checked_box="+checked_box);
    ajax.send(formdata);
}

Associated file input html

<input name="image[]" id="image" type="file" multiple style="display:none" value=""/>
Milap
  • 6,096
  • 8
  • 21
  • 44
Tiny
  • 333
  • 2
  • 5
  • 19
  • `jQuery.each($("input[name^='image']")[0].files` not appending `file` to `formdata` ? – guest271314 May 08 '15 at 18:36
  • @ guest271314 the jquery.each was something I tried? In console i can see by the file size being uploaded that there are multiple images, however there is only one image file shown in the param console, so I think I need to figure out how to get all of the file names somehow – Tiny May 08 '15 at 18:39
  • See http://stackoverflow.com/q/1175347/2801559 , http://stackoverflow.com/q/28856729/2801559 – guest271314 May 08 '15 at 18:59

0 Answers0