简体   繁体   English

HTML 页面中的目录选择器

[英]Directory Chooser in HTML page

How can I create a directory chooser in html page.如何在 html 页面中创建目录选择器。
If I use input file element I can select file only, but I need to select directory instead.如果我使用输入文件元素,我只能使用 select 文件,但我需要使用 select 目录。
I need to do this beacause the user should select a right path inside his computer.我需要这样做,因为用户应该 select 在他的计算机中有一条正确的路径。
Any solutions?任何解决方案?

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 上找到该演示 ,如果您需要更多信息,可以在此处找到。

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 (eg using SWFUpload as 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?你需要这个做什么?

In case if you are the server and the user (eg 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

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 example that uses Javascript to achieve what you want.查看此示例该示例使用 Javascript 来实现您想要的。

If the OS is windows, you can use VB scripts to access the core control files to browse for a folder.如果操作系统是windows,您可以使用VB脚本访问核心控制文件来浏览文件夹。

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.然后,在 form_onsubmit 上,我将路径值(减去文件名)复制到隐藏文件夹。 Then, set the fileInput box to "".然后,将文件输入框设置为“”。 That way, no file is uploaded.这样,就不会上传任何文件。 I don't recall the event of the fileUpload control.我不记得 fileUpload 控件的事件。 Maybe onchange.也许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.但是,如果您想反映服务器路径,则需要一种完全不同的编码方法。

This is my solution.这是我的解决方案。 It is the same as the above answers but you should notice that webkitdirectory = "true" .它与上述答案相同,但您应该注意到webkitdirectory = "true"

<input id="design" type="file" webkitdirectory = "true" directory/>

As of 2022 there is now a directory picker API:截至 2022 年,现在有一个目录选择器 API:

https://developer.mozilla.org/en-US/docs/Web/API/Window/showDirectoryPicker https://developer.mozilla.org/en-US/docs/Web/API/Window/showDirectoryPicker

async function getDir() {
  const dirHandle = await window.showDirectoryPicker();

  // run code for dirHandle
}

If you do not have too many folders then I suggest you use if statements to choose an upload folder depending on the user input details.如果您没有太多文件夹,那么我建议您使用 if 语句根据用户输入的详细信息选择上传文件夹。 Eg例如

String user= request.getParameter("username");
if (user=="Alfred"){
//Path A;
}
if (user=="other"){
//Path B;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM