简体   繁体   English

Javascript:如何将多个图像文件推送到数组中

[英]Javascript: How to push multiple image files into array

I have multiple input files 我有多个输入文件

<input type="file" name="file_name[]" id="file_id_0">
<input type="file" name="file_name[]" id="file_id_1">
<input type="file" name="file_name[]" id="file_id_2">

I want each of it store in array so i did, this is working to me 我希望每个都存储在数组中,所以我这样做,这对我有用

var imageContainer = [];
var file_name = document.getElementsByName('file_name[]');
for(var i = 0; i< file_name.length; i++){
    alert(i);
    var fileUpload = document.getElementById('file_id_'+i);
    imageContainer.push(fileUpload.files[0]);
}

var data = new FormData();
for(var b=0; b<imageContainer.length; b++){
    data.append('file_name[]', imageContainer[b]);
}

But if one of the input file is empty is I got an error Cannot read property 'files' of null. 但是,如果其中一个输入文件为空,则出现错误无法读取属性'files'的null。

So I am trying to push the files in other way but not working 所以我试图以其他方式推送文件,但不工作

var file_nameArr = [];
$('input[name="file_name[]"]').each(function(k,v){
    file_nameArr.push($(v).val()); //How do i push each files into my array?
});

But if one of the input file is empty is I got an error Cannot read property 'files' of null. 但是,如果其中一个输入文件为空,则出现错误无法读取属性'files'的null。

That doesn't mean the input is empty, it means your getElementById call didn't return an element at all. 这并不意味着input为空,这意味着你的getElementById调用根本没有返回一个元素。

There's no reason for that getElementById call, you already have the elements in the collection referenced by your file_name variable. getElementById调用没有理由,你已经拥有了file_name变量引用的集合中的元素。 Just use it, and check its length to make sure it does have at least one file in it: 只需使用它,并检查其length ,以确保它至少有一个文件:

var imageContainer = [];
var file_name = document.getElementsByName('file_name[]');
for(var i = 0; i< file_name.length; i++){
    alert(i);
    var fileUpload = file_name[i];                    // ***
    if (fileUpload.files.length) {                    // ***
        imageContainer.push(fileUpload.files[0]);
    }                                                 // ***
}

Note that the filenames will have fake paths on them. 请注意,文件名将包含伪路径。

Just check if fileUpload is not null..to do that add a condition in your for loop as pointed below: 只需检查fileUpload是否为null ..要在for循环中添加条件,如下所示:

var file_name = document.getElementsByName('file_name[]');
for(var i = 0; i< file_name.length; i++){
    alert(i);
    var fileUpload = document.getElementById('file_id_'+i);
  if(fileUpload )
    imageContainer.push(fileUpload.files[0]);
}

v is your element, so $(v) is nonse, please have a look at the example. v是你的元素,所以$(v)是不相关的,请看一下这个例子。

 var file_nameArr = []; $('input[name="file_name[]"]').each(function(k,v){ if (v.value.length > 0) file_nameArr.push(v.value); //How do i push the files into my array? else console.log('input is empty') }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" name="file_name[]" id="file_id_0"> <input type="file" name="file_name[]" id="file_id_1"> <input type="file" name="file_name[]" id="file_id_2"> <input type="file" name="file_name[]" id="file_id_3"> <input type="file" name="file_name[]" id="file_id_4"> <input type="file" name="file_name[]" id="file_id_5"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM