HTML 页面中的目录选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2809688/
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
Directory Chooser in HTML page
提问by enfix
How can I create a directory chooser in html page.
If I use input file element I can select file only, but I need to select directory instead.
I need to do this beacause the user should select a right path inside his computer.
Any solutions ?
如何在 html 页面中创建目录选择器。
如果我使用输入文件元素,我只能选择文件,但我需要选择目录。
我需要这样做,因为用户应该在他的计算机中选择正确的路径。
任何解决方案?
采纳答案by Pekka
Can't be done in pure HTML/JavaScript for security reasons.
出于安全原因,不能在纯 HTML/JavaScript 中完成。
Selecting a file for upload is the best you can do, and even then you won't get its full original path in modern browsers.
选择要上传的文件是您能做的最好的事情,即便如此,您也不会在现代浏览器中获得完整的原始路径。
You may be able to put something together using Java or Flash (e.g. using SWFUploadas a basis), but it's a lot of work and brings additional compatibility issues.
您也许可以使用 Java 或 Flash(例如,使用SWFUpload作为基础)将某些内容组合在一起,但它需要大量工作并带来额外的兼容性问题。
Another thought would be opening an iframe
showing the user's C:
drive (or whatever) but even if that's possible nowadays (could be blocked for security reasons, haven't tried in a long time) it will be impossible for your web site to communicate with the iframe (again for security reasons).
另一个想法是打开iframe
显示用户的C:
驱动器(或其他),但即使现在有可能(出于安全原因可能被阻止,很长时间没有尝试过),您的网站也不可能与 iframe 通信(再次出于安全原因)。
What do you need this for?
你需要这个做什么?
回答by Raveendra007
Try this, I think it will work for you:
试试这个,我认为它对你有用:
<input type="file" webkitdirectory directory multiple/>
You can find the demo of this at https://plus.google.com/+AddyOsmani/posts/Dk5UhZ6zfF3, and if you need further information you can find it here.
您可以在https://plus.google.com/+AddyOsmani/posts/Dk5UhZ6zfF3 上找到该演示,如果您需要更多信息,可以在此处找到 。
回答by Levon
In case if you are the server and the user (e.g. you are creating an app which works via browser and you need to choose a folder) then try to call JFileChooser
from the server when some button is clicked in the browser
如果您是服务器和用户(例如,您正在创建一个通过浏览器运行的应用程序并且您需要选择一个文件夹),则JFileChooser
在浏览器中单击某个按钮时尝试从服务器调用
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("select folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
This code snipped is from here
此代码剪断来自here
回答by KM?n
Scripting is inevitable.
脚本编写是不可避免的。
This isn't provided because of the security risk. <input type='file' />
is closest, but not what you are looking for.
由于存在安全风险,因此未提供此功能。<input type='file' />
最接近,但不是您要找的。
Checkout this examplethat uses Javascript to achieve what you want.
查看此示例,该示例使用 Javascript 来实现您想要的。
If the OS is windows, you can use VB scriptsto access the core control files to browse for a folder.
如果操作系统是windows,您可以使用VB脚本访问核心控制文件来浏览文件夹。
回答by user8004777
I did a work around. I had a hidden textbox to hold the value. Then, on form_onsubmit,
I copied the path value, less the file name to the hidden folder. Then, set the fileInput box to "". That way, no file is uploaded.
I don't recall the event of the fileUpload control. Maybe onchange. It's been a while. If there's a value, I parse off the file name and put the folder back to the box. Of, course you'd validate that the file as a valid file.
This would give you the clients workstation folder.
However, if you want to reflect server paths, that requires a whole different coding approach.
我做了一个工作。我有一个隐藏的文本框来保存值。然后,在 form_onsubmit 上,我将路径值(减去文件名)复制到隐藏文件夹。然后,将文件输入框设置为“”。这样,就不会上传任何文件。我不记得 fileUpload 控件的事件。也许onchange。有一阵子了。如果有值,我会解析文件名并将文件夹放回框中。当然,您会验证该文件为有效文件。这将为您提供客户端工作站文件夹。
但是,如果您想反映服务器路径,则需要一种完全不同的编码方法。