如何从 HTML 输入类型“文件”或任何其他方式获取文件夹目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12942436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:26:11  来源:igfitidea点击:

How to get folder directory from HTML input type "file" or any other way?

htmlfileinputdirectory

提问by Ben

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.

所以我有一个带有类型的基本表单输入,"file"但是我希望用户能够选择文件夹位置而不是文件。

How can I get this input to select a folder and not a file, or is there another way to do it?

我怎样才能得到这个输入来选择一个文件夹而不是一个文件,或者有另一种方法吗?

回答by bwindels

Stumbled on this page as well, and then found out this is possible with just javascript (no plugins like ActiveX or Flash), but just in chrome:

也偶然发现了此页面,然后发现仅使用 javascript(没有 ActiveX 或 Flash 等插件)即可实现,但仅使用 chrome:

https://plus.google.com/+AddyOsmani/posts/Dk5UhZ6zfF3

https://plus.google.com/+AddyOsmani/posts/Dk5UhZ6zfF3

Basically, they added support for a new attribute on the file input element "webkitdirectory". You can use it like this:

基本上,他们在文件输入元素“webkitdirectory”上添加了对新属性的支持。你可以这样使用它:

<input type="file" id="ctrl" webkitdirectory directory multiple/>

<input type="file" id="ctrl" webkitdirectory directory 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.

它允许您选择目录。对于支持多文件选择但不支持目录选择的浏览器,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.

当您选择一个目录时,文件可通过控件的 dom 对象 (document.getElementById('ctrl')) 获得,就像它们具有 multiple 属性一样。浏览器以递归方式将所选目录中的所有文件添加到该列表中。

You can already add the directory attribute as well in case this gets standardized at some point (couldn't find any info regarding that)

您也可以添加目录属性,以防在某些时候标准化(找不到任何相关信息)

回答by Brad Christie

You're most likely looking at using a flash/silverlight/activeX control. The <input type="file" />control doesn't handle that.

您很可能正在考虑使用 flash/silverlight/activeX 控件。该<input type="file" />控制不处理。

If you don't mind the user selecting a file as a means to getting its directory, you may be able to bind to that control's changeevent then strip the filename portion and save the path somewhere--but that's about as good as it gets.

如果您不介意用户选择文件作为获取其目录的一种方式,您可以绑定到该控件的change事件,然后剥离文件名部分并将路径保存在某处——但​​这与它得到的一样好。

Keep in mind that webpages are designed to interact with servers. Nothing about providing a local directory to a remote server is "typical" (a server can't access it so why ask for it?); however files are a means to selectively passing information.

请记住,网页旨在与服务器交互。向远程服务器提供本地目录没有什么是“典型的”(服务器无法访问它,所以为什么要要求它?);然而,文件是一种有选择地传递信息的手段。

回答by Sai

Eventhough it is an old question, this may help someone.

尽管这是一个老问题,但这可能对某人有所帮助。

We can choose multiple files while browsing for a file using "multiple"

我们可以在使用“multiple”浏览文件时选择多个文件

<input type="file" name="datafile" size="40"  multiple>