HTML 页面中的目录选择器

发布于 2024-09-01 01:47:58 字数 108 浏览 2 评论 0原文

如何在 html 页面中创建目录选择器。
如果我使用输入文件元素,我只能选择文件,但我需要选择目录。
我需要这样做,因为用户应该在他的计算机内选择正确的路径。
有什么解决办法吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

昔梦 2024-09-08 01:47:58

试试这个,我认为它会对您有用:

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

您可以在 找到此演示https://plus.google.com/+AddyOsmani/posts/Dk5UhZ6zfF3
如果您需要更多信息,您可以找到它
此处

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.

小矜持 2024-09-08 01:47:58

出于安全原因,无法在纯 HTML/JavaScript 中完成。

选择要上传的文件是您能做的最好的事情,即使这样您也无法在现代浏览器中获得其完整的原始路径。

您也许可以使用 Java 或 Flash 将某些内容组合在一起(例如使用 SWFUpload 作为基础),但它是一个大量的工作并带来额外的兼容性问题。

另一个想法是打开一个 iframe 显示用户的 C: 驱动器(或其他),但即使现在这是可能的(可能出于安全原因被阻止,还没有尝试过)很长一段时间)您的网站将无法与 iframe 通信(同样出于安全原因)。

你需要这个做什么?

Can't be done in pure HTML/JavaScript for security reasons.

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 SWFUpload as a basis), but it's a lot of work and brings additional compatibility issues.

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

What do you need this for?

淡笑忘祈一世凡恋 2024-09-08 01:47:58

截至 2022 年,现在有一个目录选择器 API:

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

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

  // run code for dirHandle
}

As of 2022 there is now a directory picker API:

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

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

  // run code for dirHandle
}
森末i 2024-09-08 01:47:58

这是我的解决方案。它与上面的答案相同,但您应该注意到 webkitdirectory = "true"

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

This is my solution. It is the same as the above answers but you should notice that webkitdirectory = "true".

<input id="design" type="file" webkitdirectory = "true" directory/>
梦里梦着梦中梦 2024-09-08 01:47:58

如果您是服务器和用户(例如,您正在创建一个通过浏览器运行的应用程序,并且您需要选择一个文件夹),则尝试在单击某个按钮时从服务器调用 JFileChooser浏览器

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("select folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);

这段代码摘自这里

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

朱染 2024-09-08 01:47:58

我做了一个解决办法。我有一个隐藏的文本框来保存该值。然后,在 form_onsubmit 上,
我将路径值(减去文件名)复制到隐藏文件夹。然后,将文件输入框设置为“”。这样,就不会上传任何文件。
我不记得 fileUpload 控件的事件。也许 onchange。已经有一段时间了。如果有值,我会解析文件名并将文件夹放回框中。当然,您会验证该文件是否为有效文件。
这将为您提供客户端工作站文件夹。
但是,如果您想反映服务器路径,则需要完全不同的编码方法。

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.

独留℉清风醉 2024-09-08 01:47:58

由于安全风险,HTML 不提供此功能。 最接近,但不是您要查找的内容。

如果您仍在 Windows 10 上使用 IE11,您可以尝试此示例 使用 ActiveX 控件 来实现您想要的。

同样,如果操作系统是 Windows,您可以使用 VB脚本来访问核心控制文件以浏览文件夹。

This isn't provided by HTML because of the security risk. <input type='file' /> is closest, but not what you are looking for.

If you're still using IE11 on Windows 10, you may try this example that uses an ActiveX control to achieve what you want.

Again if the OS is Windows, you can use VB scripts to access the core control files to browse for a folder.

尽揽少女心 2024-09-08 01:47:58

如果您没有太多文件夹,那么我建议您使用 if 语句根据用户输入的详细信息选择上传文件夹。
例如

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

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

String user= request.getParameter("username");
if (user=="Alfred"){
//Path A;
}
if (user=="other"){
//Path B;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文