3

I want to create a browse button in my web page to select directory and not file. I know that input type file won't work here but is there any way to do it with Javascript. I want to get the filepath of client machine which is possible in IE but other browser are not supporting but that is fine for me.

The way I got stuck is how to get file directory in button.

Below is the code I am using to call applet from browser but I am getting Detected from bootclasspath: C:\PROGRA~1\Java\jre7\lib\deploy.jar error in browser. I have compiled class file using Java 1.5

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

Code

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 ");
    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
pise
  • 849
  • 6
  • 24
  • 51
  • This is not possible - the only control which has access to the client machine is the `file` input, and that can only select a file, not a folder. Why do you need this? – Rory McCrossan Aug 20 '13 at 10:19
  • 2
    Duplicate of this question: http://stackoverflow.com/questions/2809688/directory-chooser-in-html-page – Colin Basnett Aug 20 '13 at 10:19
  • webkitdirectory attribute and other modern browsers equivalents `` – A. Wolff Aug 20 '13 at 10:22
  • Closers: This is **NOT** a duplicate of the "Directory chooser in HTML page" question. That question has answers that are 3 years old, and are no longer correct. The answer provided by @Spikeh in _this_ thread is most accurate. – Ray Nicholus Aug 20 '13 at 18:07

2 Answers2

3

Why the hell are you calling this in the your paint method? This is likely trying creating to create new windows EVERY TIME the applet is painted.

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

Instead, create a JButton in your init method and attach an ActionListener to it...

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);
}

You might also like to take a look at What Applets Can and Cannot Do

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • problem is not resolved and still window is not getting close even after putting the code in init method. And also there is a small change in my jsp code `. Plugin tag OBJECT or EMBED not supported by browser. ` Please advice – pise Aug 27 '13 at 09:17
  • It is working now after putting the code in init, actually I changed the code created a jar as well as signed jar but forgot to replace the signed jar after changing the code. Thank You very much my problem is resolved. – pise Aug 27 '13 at 09:31
2

The only way you can get a local browse dialogue in a web browser is either by using <input type="file"/>, or by using a Java Applet or Adobe Flash plugin. There is no built in way to get a directory reference from JS in a web browser.

Also, you cannot read the contents of a client's hard disk, or even initiate a browse dialogue via JavaScript. If you were able to, it would impose considerable security issues.

In reference to reading a directory, take a look at the following posts:

Local file access with javascript

Getting content of a local file without uploading

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

By the sound of it, you're going to need to write a flash plugin that lets you select a directory locally. Your users will be given a security warning when downloading the plugin, though.

Edit:

There's also the webkit based method, but this will only work in webkit based browsers (Chrome, Safari etc).

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

Community
  • 1
  • 1
Spikeh
  • 3,540
  • 4
  • 24
  • 49
  • @RayNicholus I am getting Detected from bootclasspath: C:\PROGRA~1\Java\jre7\lib\deploy.jar error in browser while runing applet. I have compiled my applet class using Java 1.5. I am adding my JSP and class code above. – pise Aug 22 '13 at 05:32
  • Can someone please tell me what is wrong. when I hit the jsp from server I got classnotfound exception. But if I run in client machine I got error click details with red mark (C:\PROGRA~1\Java\jre7\lib\deploy.jar) – pise Aug 22 '13 at 07:07
  • ok, I got the solution how to display JFileChooser in applet. Create your own appletpermission.policy make the yourApplet.jar signed and in your code point your code to appletpermission.policy (System.setProperty("java.security.policy", "file:/C:/Program Files/Java/jre6/lib/security/appletpermission.policy");). For more Information refer [link]http://www.pawlan.com/monica/articles/signedapps/ – pise Aug 26 '13 at 11:35
  • But after this I am facing new problem when I click on open or close button I get the value in my code but the JfileChooser window is not closing. – pise Aug 26 '13 at 11:36