HTML:
<input accept="image/*" class="" id="my_pics" multiple="multiple" name="pics" required="true" type="file" />
Run Code Online (Sandbox Code Playgroud)
JS:
var files = document.getElementById('my_pics').files;
files.splice(1,2)
//TypeError: Object #<FileList> has no method 'splice'
delete files.item(2)
// true, but nothing happens with "2"
delete files[2]
// false, result is the same
Run Code Online (Sandbox Code Playgroud)
我有一个包含多个文件的表单,我需要在服务器上只在一个输入中上传有限数量的图像(例如,5),但我的代码不起作用.
怎么解决?谢谢.
您不能删除它,因为它是File对象中指向的FileList对象,因此出现了上面的错误。但是,您可以遍历FileList并仅将所需的项目推入数组,然后可以对其进行修改:
var files = document.getElementById('my_pics').files;
var newList = [];
for(var i = 0; i < files.length; i++)
{
if(i !== 2)
{
newList.push(files.item(i));
}
}
Run Code Online (Sandbox Code Playgroud)
从此答案中可以看到,您不能delete成为实际对象。只能删除对该对象的引用。因此,即使它适用于该FileList对象(不是),也只会将该元素 设置为undefined。
| 归档时间: |
|
| 查看次数: |
12022 次 |
| 最近记录: |