如何以编程方式更改文件输入的 FileList? [英] How to change a file input's FileList programmatically?
问题描述
我有这个 "file"
类型的 input
,我想更改它的 files
列表.示例:
I have this input
of type "file"
, and I want to change its files
list. Example:
<input type = "file" id = "fileinput" />
<script type = "text/javascript">
document.getElementById("fileinput").files = [10];
</script>
问题在于未设置 fileinput
元素的 files
列表.我该怎么做?
The problem is that the fileinput
element's files
list is not set. How do I do it?
推荐答案
出于安全原因,浏览器会阻止 javascript 更改将要上传的文件:只有用户可以通过用户界面选择文件.这是为了防止恶意脚本在用户不知情的情况下上传/etc/passwd
.
For security reasons, browsers prevent javascript from changing the files which will be uploaded: only the user can select files via the user interface. This is to prevent an evil script to upload /etc/passwd
, for example, without the user knowing.
一个例外是在表单上调用reset"将清除文件或文件列表,但您永远无法以编程方式添加.
The one exception is that calling "reset" on the form will clear the file or filelist, but you can never add to programmatically.
这篇关于如何以编程方式更改文件输入的 FileList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!