1

I am looking for a way to save a couple of files created with JavaScript on the clients computer. I want him to be able to choose a folder (similar to open/save folder dialog), and then write the files there. This obviously requires three things:

  • Ability to popup such a dialog.
  • Ability to write on the client's computer (signed applets?)
  • Some way to interact with JavaScript because it knows the filenames and contents to write.

Is this possible or should I just stop dreaming?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tower
  • 98,741
  • 129
  • 357
  • 507

3 Answers3

3

It is possible with a signed applet.

I'd probably do it the other way around, let the javascript code pass all the data to the applet and the applet should open a FileDialog and write the file.

Calling javascript from java can be achieved via `JSObject, calling Java from Javascript is explained here

biziclop
  • 48,926
  • 12
  • 77
  • 104
3

It sure is possible.

One way is to use a signed applet, but if your users are willing to upgrade to Java 1.6.0_10+, a signed applet is not needed. Since 1.6.0_10 (the Next Generation Java Plug-In), JNLP API services are available to embedded applets. Here is a demo. of the JNLP file services.

I see the other reply has already covered the applet/JS interaction.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Can I run those without downloading them first (Chrome)? – Tower Feb 04 '11 at 19:53
  • Only just noticed the Feb. 4 comment. And ..huh? Are you referring to the JNLP file? The download should be relatively transparent to the end user, especially for a well deployed, **embedded** applet! – Andrew Thompson Feb 26 '11 at 04:09
1

If your applet is signed you can do everything that you can do in regular application. You can open "File dialog" but it will be the Java's dialog, not the same that is opened when user pushes "browse" button of tag <intput type"file"/>. Obviously signed applet can perform read and write operations with user's disk.

All applets (either signed or unsigned) may call javascript using JSObject. You just have to say MAYSCRIPT="true" into applet tag.

I think that you cannot open the browser's "file open" dialog applet. Actually you even cannot open it using regular javascript. (I'd be happy to know that I am wrong at this point but I spent some time trying to do this and failed).

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • You can open it via JavaScript depending on the platform, but only the open dialog and not the save dialog. In Firefox 4 you can directly trigger a click call on the input and in other browsers you can hide the input and place it below some other element that when clicked opens the dialog. – Tower Jan 31 '11 at 20:11
  • MAYSCRIPT is an odd attribute. It does not require the "=true" part, it just needs to be added to the applet element itself. – Andrew Thompson Jan 31 '11 at 21:12