how to change the file input's filelist

依然范特西╮ 提交于 2019-12-18 23:37:38

问题


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>

The problem is that the fileinput element's files list is not set. How do I do it?


回答1:


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.

The one exception is that calling "reset" on the form will clear the file or filelist, but you can never add to programmatically.




回答2:


What you want is using the multiple attribute on the input element. That way, in newer browsers user will be able to select multiple files to upload.



来源:https://stackoverflow.com/questions/5632629/how-to-change-the-file-inputs-filelist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!