如何从文件列表中删除文件
我正在使用 HTML5 构建一个拖放上传 Web 应用程序,我将文件拖放到 div 上,当然还获取 dataTransfer 对象,这给了我 文件列表。
现在我想删除一些文件,但我不知道如何删除,或者是否可能删除。
最好我想从 FileList 中删除它们;我对它们没有任何用处。但如果这是不可能的,我是否应该在与 FileList 交互的代码中写入检查?这看起来很麻烦。
I'm building a drag-and-drop-to-upload web application using HTML5, and I'm dropping the files onto a div and of course fetching the dataTransfer object, which gives me the FileList.
Now I want to remove some of the files, but I don't know how, or if it's even possible.
Preferably I'd like to just delete them from the FileList; I've got no use for them. But if that's not possible, should I instead write in checks in code that interacts with the FileList? That seems cumbersome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
我混合了许多开发人员的解决方案并得出了这个解决方案。
它在删除后更改原始数组列表,这意味着如果我们想保存图像,那么我们可以这样做。
******这是 html 代码 ******
******** 这是 CSS 代码 ********
I mix the solutions of many developers and reach to this solution.
It change the original array list after deletion which means if we want to save the images then we can do so.
*******This is html code ******
******* This is CSS code ********
我正在使用 TypeScript,但由于强类型检查,它在每一步都会抱怨。以下代码在 Svelte 中对我有用。它与斯维尔特无关。所以它应该适用于所有使用 TS 的库/框架。
I am using TypeScript and it complained at every steps because of strong type checking. The following code worked for me in Svelte. It has nothing to do with Svelte. So it should work with all libraries/frameworks using TS.
显然,到 2024 年,这仍然是一件不太明显的事情。真正修改
input.files
列表的唯一方法似乎是将其替换为DataTransfer.files
如最佳答案所示。这是几乎相同的代码,但使用 Array.reduce 编写:
这是代码的更简洁版本,但有点难以理解:
Apparently this is still a not-so-obvious thing in 2024. The only way to truly modify the
input.files
list seems to be by replacing it with aDataTransfer.files
as demonstrated in the best answer.Here is pretty much the same code but written using
Array.reduce
:Here is a more concise version of the code but a bit harder to grasp:
您可能希望创建一个数组并使用它来代替只读文件列表。
之后,根据您的列表而不是内置列表进行上传。我不确定您正在使用的上下文,但我正在使用我发现的 jquery 插件,我要做的就是获取插件的源代码并使用
将其放入页面中标签。然后在源代码上方添加了我的数组,以便它可以充当全局变量,并且插件可以引用它。
然后只需更换参考文献即可。
我认为这还可以让您添加拖动和拖动功能。再次删除,如果内置列表是只读的,那么您还能如何将删除的文件放入列表中?
:))
You may wish to create an array and use that instead of the read-only filelist.
After that point do your uploading against your list instead of the built in list. I am not sure of the context you are working in but I am working with a jquery plugin I found and what I had to do was take the plugin's source and put it in the page using
<script>
tags. Then above the source I added my array so that it can act as a global variable and the plugin could reference it.Then it was just a matter of swapping out the references.
I think this would allow you to also add drag & drop as again, if the built in list is read-only then how else could you get the dropped files into the list?
:))
我这样解决
I solve it this way
在 vue.js 中:
In vue js :
我只是将输入类型更改为文本并返回到文件:D
I just change the type of input to the text and back to the file :D
如果您只想删除几个选定的文件:则不能。您链接到的文件 API 工作草案包含一条注释:
阅读了一些 HTML 5 工作草案,我发现了 通用
input
元素API。看来您可以通过将input
对象的value
属性设置为空字符串来删除整个文件列表,例如:顺便说一句,文章使用网络应用程序中的文件也可能令人感兴趣。
If you want to delete only several of the selected files: you can't. The File API Working Draft you linked to contains a note:
Reading a bit of the HTML 5 Working Draft, I came across the Common
input
element APIs. It appears you can delete the entire file list by setting thevalue
property of theinput
object to an empty string, like:BTW, the article Using files from web applications might also be of interest.
由于 JavaScript FileList 是只读的,无法直接操作,
最佳方法
您必须循环遍历
input.files
,同时将其与index
进行比较您要删除的文件的名称。同时,您将使用new DataTransfer()
设置一个新的文件列表,不包括要从文件列表中删除的文件。通过这种方法,
input.files
本身的值会发生变化。替代方法
另一种简单的方法是将FileList转为数组,然后拼接。
但这种方法不会改变
input.files
Since JavaScript FileList is readonly and cannot be manipulated directly,
BEST METHOD
You will have to loop through the
input.files
while comparing it with theindex
of the file you want to remove. At the same time, you will usenew DataTransfer()
to set a new list of files excluding the file you want to remove from the file list.With this approach, the value of the
input.files
itself is changed.ALTERNATIVE METHOD
Another simple method is to convert the FileList into an array and then splice it.
But this approach will not change the
input.files
这个问题已被标记为已回答,但我想分享一些可能有助于其他人使用 FileList 的信息。
将 FileList 视为数组会很方便,但排序、移位、弹出和切片等方法不起作用。正如其他人所建议的,您可以将 FileList 复制到数组中。但是,有一个简单的单行解决方案来处理此转换,而不是使用循环。
在 FF、Chrome 和 IE10+ 中测试正常
This question has already been marked answered, but I'd like to share some information that might help others with using FileList.
It would be convenient to treat a FileList as an array, but methods like sort, shift, pop, and slice don't work. As others have suggested, you can copy the FileList to an array. However, rather than using a loop, there's a simple one line solution to handle this conversion.
Tested OK in FF, Chrome, and IE10+
如果您的目标是常青浏览器(Chrome、Firefox、Edge,但也适用于 Safari 9+)或者您可以负担得起 polyfill,则可以使用 Array.from() 将 FileList 转换为数组像这样:
然后就可以像处理任何其他数组一样轻松处理
File
数组。If you are targeting evergreen browsers (Chrome, Firefox, Edge, but also works in Safari 9+) or you can afford a polyfill, you can turn the FileList into an array by using
Array.from()
like this:Then it's easy to handle the array of
File
s like any other array.由于我们处于 HTML5 领域,这就是我的解决方案。要点是将文件推送到 Array,而不是将它们保留在 FileList 中,然后使用 XHR2,将文件推送到 FormData 对象。下面的例子。
Since we are in the HTML5 realm, this is my solution. The gist is that you push the files to an Array instead of leaving them in a FileList, then using XHR2, you push the files to a FormData object. Example below.
我发现很快&对此的简短解决方法。在许多流行的浏览器(Chrome、Firefox、Safari)中进行了测试;
首先,您必须将 FileList 转换为数组
才能使用此删除特定元素
I have found very quick & short workaround for this. Tested in many popular browsers (Chrome, Firefox, Safari);
First, you have to convert FileList to an Array
to delete the particular element use this
我知道这是一个老问题,但它在搜索引擎上的排名很高。
FileList 对象中的属性无法删除,但至少在 Firefox 上它们可以更改。我的解决方法是向通过检查的文件添加属性
IsValid=true
,向未通过检查的文件添加属性IsValid=false
。然后我循环遍历列表以确保只有
IsValid=true
的属性被添加到 FormData 中。I know this is an old question but it's ranking high on search engines in regards to this issue.
properties in the FileList object cannot be deleted but at least on Firefox they can be changed. My workaround this issue was to add a property
IsValid=true
to those files that passed check andIsValid=false
to those that didn't.then I just loop through the list to make sure that only the properties with
IsValid=true
are added to FormData.这是临时的,但我遇到了同样的问题,我用这种方式解决了。就我而言,我是通过 XMLHttp 请求上传文件,因此我能够通过 formdata 附加来发布 FileList 克隆数据。 功能是您可以根据需要多次拖放或选择多个文件(再次选择文件不会重置克隆的文件列表),从(克隆的)文件列表中删除您想要的任何文件,然后通过xmlhttprequest 剩下的任何东西。 这就是我所做的。这是我在这里的第一篇文章,所以代码有点混乱。对不起。啊,我必须使用 jQuery 而不是 Joomla 脚本中的 $。
现在是这个的 html 和样式。我是一个新手,但这一切实际上对我有用,并且花了我一段时间才弄清楚。
那种风格。我必须标记其中一些!重要的是要覆盖 Joomla 行为。
我希望这有帮助。
This is extemporary, but I had the same problem which I solved this way. In my case I was uploading the files via XMLHttp request, so I was able to post the FileList cloned data through formdata appending. Functionality is that you can drag and drop or select multiple files as many times as you want (selecting files again won't reset the cloned FileList), remove any file you want from the (cloned) file list, and submit via xmlhttprequest whatever was left there. This is what I did. It is my first post here so code is a little messy. Sorry. Ah, and I had to use jQuery instead of $ as it was in Joomla script.
Now the html and styles for this. I'm quite a newbie but all this actually worked for me and took me a while to figure it out.
The styles for that. I had to mark some of them !important to override Joomla behavior.
I hope this helps.
感谢@Nicholas Anderson 简单而直接,这是您应用的代码并使用 jquery 在我的代码中工作。
HTML 。
JS代码
Thanks @Nicholas Anderson simple and straight , here is your code applied and working at my code using jquery.
HTML .
JS CODE
可能有一种更优雅的方法来做到这一点,但这是我的解决方案。使用 Jquery
基本上你可以获取输入的值。克隆它并将克隆代替旧的。
There might be a more elegant way to do this but here is my solution. With Jquery
Basically you cleat the value of the input. Clone it and put the clone in place of the old one.
如果您有幸向数据库发送带有文件的 post 请求,并且您的 DOM 中有要发送的文件,
您可以简单地检查文件列表中的文件是否存在于您的 DOM 中,当然如果并不是您只是不将该元素发送到数据库。
If you have the luck to be sending a post request to the database with the files and you have the files you want to send in your DOM
you can simply check if the file in the file list is present in your DOM, and of course if it's not you just don't send that element to de DB.
我意识到这是一个相当老的问题,但是我正在使用 html 多文件选择上传来对任意数量的文件进行排队,这些文件可以在提交之前在自定义 UI 中有选择地删除。
将文件保存在如下变量中:
使用“删除文件”创建 UI:
提交到服务器:
I realize this is a pretty old question, however I am using an html multiple file selection upload to queue any number of files which can be selectively removed in a custom UI before submitting.
Save files in a variable like this:
Create UI with "remove a file":
Submit to server: