如何从HTML输入类型“文件”获取文件夹目录或任何其他方式? [英] How to get folder directory from HTML input type "file" or any other way?

查看:725
本文介绍了如何从HTML输入类型“文件”获取文件夹目录或任何其他方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个基本的表单输入,类型为

如何获取此输入来选择一个文件夹而不是一个文件,还是有另一种方法?

解决方案

在这个页面上也绊倒了,然后发现这是可能的只是javascript(没有插件,如ActiveX或Flash),但只是在chrome:



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



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



< input type =fileid =ctrlwebkitdirectory directory multiple />



它允许您选择目录。多重属性对于支持多个文件选择而不是目录选择的浏览器是一个很好的回退。



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



您也可以添加目录属性,以防某些情况下进行标准化(无法找到有关的信息)


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?

解决方案

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:

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:

<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.

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输入类型“文件”获取文件夹目录或任何其他方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆