输入类型=文件多个,删除项目 [英] input type=file multiple, delete items

查看:89
本文介绍了输入类型=文件多个,删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

html:

<input accept="image/*" class="" id="my_pics" multiple="multiple" name="pics" required="true" type="file" />

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

我有一个包含多个文件的表单,我需要在服务器上传只有限数量的一个输入中的图像(例如,5),但我的代码不起作用。

I have a form with multiple file, I need to upload on server only limited number of images (for example, 5) in one input, but my code doesnt work.

如何解决?谢谢。

推荐答案

你不能删除它,因为它是文件对象在 FileList 对象中指向,因此上面的错误。但是,您可以遍历 FileList 并且只将您想要的项目推送到数组,然后您可以修改:

You can't delete that because it's a File object pointed to in the FileList object, hence the error above. However, you can iterate over the FileList and only push the items that you want onto an array, which you can then modify:

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));
    }
}

这个答案,你不能删除实际对象。只能删除对象的引用。因此,即使它适用于 FileList 对象,它也没有,它只会设置该元素等于 undefined

As you can see from this answer, you can't delete the actual object. Only the reference to the object can be deleted. So, even if it worked for the FileList object, which it doesn't, it would only set that element equal to undefined.

这篇关于输入类型=文件多个,删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆