如何使用type =“ file”在新输入中找到多个选定文件?
常规输入
:<input type="file" name="images[]" id="images"multiple="multiple">
由js创建的用于分配的输入:
<input type="file" name="images[]" id="images" multiple="multiple">
<input type="file" name="images[]" id="images" multiple="multiple">
<input type="file" name="images[]" id="images" multiple="multiple">
答案 0 :(得分:0)
我将如何处理这个问题:
// This is not ES6, but the idea is the same
var selectedFiles = [];
// Get all input[file] fields
var inputs = document.querySelectorAll("input[type='file']");
// Loop trough every match
for (i=0; i<inputs.length; i++){
console.log(inputs[i]);
// Check if this input have any selected files
if (inputs[i].files.length > 0) {
// loop to check every file
for (var i = 0; i <= inputs.files.length - 1; i++) {
var fname = inputs.files.item(i).name; // THE NAME OF THE FILE.
var fsize = inputs.files.item(i).size; // THE SIZE OF THE FILE.
selectedFiles.push([fname,fsize]);
}
}
// Show the contents
console.log("Files: ", selectedFiles);
}
希望它会有所帮助:)