Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  109] [ 1]  / answers: 1 / hits: 68637  / 10 Years ago, wed, january 14, 2015, 12:00:00

html markup:



<input id=fileSelect type=file id=file name=files[] multiple=multiple accept=image/* />


I am uploading multiples files with php. I want to make an array of upload files and send to server with ajax. how to make an array of the multiple selected files?



JavaScript:



jQuery.ajax({
url: 'insertfiles.php',
type: POST,
data: {
file: // array of selected files.
},
success: function(data){
},
error: function(data){
alert( 'Sorry.' );
}
});

More From » php

 Answers
28

Modern browsers that support the HTML5 file stuff have in the <input> element a files property.
That will give you a file list reference, which has a length property.



As the property is already an array so you just need to access it or iterate through it.



JS



var input = document.getElementById('id');
console.log(input.files);

for (var i = 0; i < input.files.length; i++) {
console.log(input.files[i]);
}

[#68215] Sunday, January 11, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carolinabritneyp

Total Points: 75
Total Questions: 102
Total Answers: 105

Location: Armenia
Member since Fri, Apr 16, 2021
3 Years ago
;