pise
pise

Reputation: 865

Browse button to select directory

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

Upvotes: 3

Views: 20910

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347214

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

Upvotes: 3

Spikeh
Spikeh

Reputation: 3695

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?

Upvotes: 2

Related Questions