问题描述
所以我有一个类型为 "file"
的基本表单输入,但是我希望用户能够选择文件夹位置而不是文件.
So I have a basic form input with the type "file"
however I want the user to be able to select a folder location and not a file.
我怎样才能得到这个输入来选择一个文件夹而不是一个文件,或者还有其他的方法吗?
How can I get this input to select a folder and not a file, or is there another way to do it?
推荐答案
也偶然发现了这个页面,然后发现只用 javascript 就可以做到这一点(没有 ActiveX 或 Flash 等插件)
Stumbled on this page as well, and then found out this is possible with just javascript (no plugins like ActiveX or Flash)
基本上,他们在文件输入元素webkitdirectory"上添加了对新属性的支持.你可以这样使用它:
Basically, they added support for a new attribute on the file input element "webkitdirectory". You can use it like this:
它允许您选择目录.对于支持多文件选择但不支持目录选择的浏览器,multiple 属性是一个很好的后备.
It allows you to select directories. The multiple attribute is a good fallback for browsers that support multiple file selection but not directory selection.
当您选择一个目录时,文件可通过控件的 dom 对象 (document.getElementById('ctrl')) 使用,就像它们具有 multiple 属性一样.浏览器将所选目录中的所有文件递归添加到该列表中.
When you select a directory the files are available through the dom object for the control (document.getElementById('ctrl')), just like they are with the multiple attribute. The browsers adds all files in the selected directory to that list recursively.
您也可以添加目录属性,以防在某些时候标准化(找不到任何相关信息)
You can already add the directory attribute as well in case this gets standardized at some point (couldn't find any info regarding that)
这篇关于如何从 HTML 输入类型“文件"中获取文件夹目录或任何其他方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!