【问题标题】:Browse button to select directory浏览按钮选择目录
【发布时间】:2013-08-20 10:16:26
【问题描述】:

我想在我的网页中创建一个浏览按钮来选择目录而不是文件。我知道输入类型文件在这里不起作用,但有什么办法可以用 Javascript 来做到这一点。我想获取客户端机器的文件路径,这在 IE 中是可能的,但其他浏览器不支持,但这对我来说很好。

我卡住的方法是如何在按钮中获取文件目录。

下面是我用来从浏览器调用小程序的代码,但我从引导类路径中检测到:浏览器中的 C:\PROGRA~1\Java\jre7\lib\deploy.jar 错误。我已经使用 Java 1.5 编译了类文件

<applet code="com.life.draw.BrowsePage.class"></applet>

代码

public class BrowsePage extends JApplet {
@Override
public void paint(Graphics g) {
    // TODO Auto-generated method stub
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle("Browse the folder to process");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
        System.out.println("getSelectedFile() : "+ chooser.getSelectedFile());
    } else {
        System.out.println("No Selection ");
    }
}
}

【问题讨论】:

  • 这是不可能的 - 可以访问客户端计算机的唯一控件是 file 输入,它只能选择一个文件,而不是一个文件夹。为什么需要这个?
  • 这个问题的重复:stackoverflow.com/questions/2809688/…
  • webkitdirectory 属性和其他现代浏览器等价物&lt;input type="file" webkitdirectory&gt;
  • Closers:这是 NOT“HTML 页面中的目录选择器”问题的副本。该问题的答案已存在 3 年,不再正确。 @Spikeh 在 this 线程中提供的答案是最准确的。

标签: java applet


【解决方案1】:

你到底为什么在 paint 方法中调用它?这可能是在每次小程序为painted 时尝试创建新窗口。

public void paint(Graphics g) {
    // TODO Auto-generated method stub
    JFileChooser chooser = new JFileChooser();
    /*...*/

相反,在您的 init 方法中创建一个 JButton 并将 ActionListener 附加到它...

public void init() {
    setLayout(new GridBagLayout());
    JButton browse = new JButton("...");
    browse.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new java.io.File("."));
            chooser.setDialogTitle("Browse the folder to process");
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            chooser.setAcceptAllFileFilterUsed(false);

            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
                System.out.println("getSelectedFile() : "+ chooser.getSelectedFile());
            } else {
                System.out.println("No Selection ");
            }
        }
    });
    add(browse);
}

您可能还想看看What Applets Can and Cannot Do

【讨论】:

  • 问题没有解决,即使将代码放入 init 方法后,窗口仍然没有关闭。而且我的jsp代码也有一点小改动&lt;jsp:plugin type="applet" archive="SMyApplet.jar" code="com.life.draw.BrowseDirectory.class" codebase="http://189.28.156.110:8080/imageflow/" jreversion="1.6" width="600" height="320" &gt;. &lt;jsp:fallback&gt; Plugin tag OBJECT or EMBED not supported by browser. &lt;/jsp:fallback&gt; &lt;/jsp:plugin&gt;请指教
  • 将代码放入init后现在可以工作,实际上我更改了代码创建了一个jar以及签名的jar,但在更改代码后忘记替换签名的jar。非常感谢,我的问题已经解决了。
【解决方案2】:

在网络浏览器中获得本地浏览对话的唯一方法是使用&lt;input type="file"/&gt;,或者使用Java Applet 或Adobe Flash 插件。没有内置方法可以在 Web 浏览器中从 JS 获取目录引用。

此外,您无法读取客户端硬盘的内容,甚至无法通过 JavaScript 启动浏览对话。如果可以的话,它会带来相当大的安全问题。

关于读取目录,请看以下帖子:

Local file access with javascript

Getting content of a local file without uploading

Javascript: Getting the contents of a local server-side file

听上去,您将需要编写一个 Flash 插件,让您可以在本地选择一个目录。不过,您的用户在下载插件时会收到安全警告。

编辑:

还有基于 webkit 的方法,但这仅适用于基于 webkit 的浏览器(Chrome、Safari 等)。

How do I use Google Chrome 11's Upload Folder feature in my own code?

【讨论】:

  • @RayNicholus 我从引导类路径中检测到:运行小程序时浏览器中出现 C:\PROGRA~1\Java\jre7\lib\deploy.jar 错误。我已经使用 Java 1.5 编译了我的小程序类。我在上面添加我的 JSP 和类代码。
  • 有人可以告诉我什么是错的。当我从服务器点击 jsp 时,我得到了 classnotfound 异常。但是如果我在客户端机器上运行,我会收到带有红色标记的错误单击详细信息 (C:\PROGRA~1\Java\jre7\lib\deploy.jar)
  • 好的,我得到了如何在小程序中显示 JFileChooser 的解决方案。创建您自己的 appletpermission.policy 使 yourApplet.jar 签名并在您的代码中将您的代码指向 appletpermission.policy (System.setProperty("java.security.policy", "file:/C:/Program Files/Java/jre6/ lib/security/appletpermission.policy");)。更多信息请参考[链接]pawlan.com/monica/articles/signedapps
  • 但是在此之后,当我单击打开或关闭按钮时,我遇到了新问题,我在代码中得到了值,但 JfileChooser 窗口没有关闭。
猜你喜欢
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 2013-04-25
  • 1970-01-01
  • 2012-01-24
  • 2015-08-27
  • 1970-01-01
相关资源
最近更新 更多