0

I would like to create a file destination chooser as like choosing save destination of download file from browser. But I have no idea how to figure it out.

I want to create some files and save them at a place that choose from user. Similar with create new Java Class in Eclipse IDE and browser for source folder.

I had found many File-Uploaders but these were browse for specific files not folders.

Any suggestions for my problem? I would really appreciate your suggestions.

Especially I am more prefer creating with Java (but not swing because I am creating web project) but I can also used by JavaScript or some other useful libs.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Cataclysm
  • 7,592
  • 21
  • 74
  • 123
  • @Andrew Thompson , I think I will spend sometimes on it. I am still a beginner in java. Really noob in java EE. So , I can't say anything about JApplet because no exp on it. Can it be satisfy my problem ? I know a little about JFileChooser. – Cataclysm Jan 08 '14 at 06:17
  • *"Can it be satisfy my problem ?"* ...do you *really* think I would suggest it if I did *not* think so? – Andrew Thompson Jan 08 '14 at 06:21
  • @Andrew Thompson , sorry sir ! you described link to JApplet so I got ambiguity with my question. – Cataclysm Jan 08 '14 at 06:23

5 Answers5

2

If you want to customize the way a file is downloaded in a browser, that CANNOT be done via standard browser functionality (HTML/CSS/Javascript).

The browser depending on the value of the Content-Disposition HTTP header sent in the response will either do one of two things upon file download:

  • Try to parse the file depending on it's extension, render the file if it's HTML, display it if's xml etc
  • If Content-Dispositionis set to attachment, it will open a very basic popup asking where to save the file, there is no control or customization possible for the popup.

If you want more functionality than that in a browser, then you need an applet (or some other plugin the user would have to install).

But due to security problems for Java in the browser for a large part of last year, these days browsers are reluctant to run applets and they ask for many user confirmations, even if the user already has Java installed.

If the users of the site have to download Java and click several popups in order for the file download widget to work, they will probably navigate away from the site.

Applets are these days obsolete technology that is best avoided, so in a web browser the basic popup to save files is currently the most practical solution.

Angular University
  • 42,341
  • 15
  • 74
  • 81
  • I have update the answer for file download instead, by setting a header in the response you can trigger the default browser download functionality which is currently the most practical solution. – Angular University Jan 14 '14 at 20:14
  • Now I realized that I can't save a file on the client's computer from the server. It's totally impossible. I would have to serve the file as a regular file download, and the browser will then offer the user to choose the directory to save the file in. – Cataclysm Jan 16 '14 at 07:39
1

..Java (but not Swing because I am creating web project)

Swing provides JApplet, so use that and a JFileChooser. See How to Use File Choosers for details and examples.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"Really noob in java"* Then you have a steep learning curve ahead! See [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/) for my 'bit of a rant' on applets. Note that blog article does not even account for the fact that this code will need to be digitally signed! ..Actually it does mention it in passing. – Andrew Thompson Jan 08 '14 at 06:23
  • 1
    wow ! Today I got a new teacher in my life. Thanks in advance sir. Thanks for useful answer and tips. I greatly appreciate to you. – Cataclysm Jan 08 '14 at 06:25
1

If you give the web browser the command to go to a URL and the response comes back:

      response.addHeader("Content-Disposition", "attachment; filename=" + "\"" +doc.getFileName()+"\"");
      response.setContentLength(doc.getData().length);
      response.getOutputStream().write(doc.getData());
      response.flushBuffer();

(Where doc.getData() is a byte[] array in this case).

Then the web browser will detect the attachment, infer the type from the filename, and display to the user a "save" dialogue box to allow them to decide where to save it too.

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

You can use JFileCHooser with FIle class methods such as

File a=JFileChooser.getFile();

To know more about file class and JFileChooser refer links below

For file class

FileChooser

Santhosh
  • 8,181
  • 4
  • 29
  • 56
Tilak Patidar
  • 115
  • 1
  • 4
0

I'm not sure whether this is an answer/hint for ur problem. Just try

Using the tag in HTML to browse for a file. The below example will specifically select MS Excel files <input type="file" name="excelFile" value="Browse For Excel File" id="excelfileId" onChange="handleFiles(this.files)" accept="application/vnd.ms-excel"/>

For selecting a folder (and not a file), There isnt any direct tag's. Hope this code sample can be referred.

A similar topic/query is discussed here

Community
  • 1
  • 1