简体   繁体   English

Java小程序能否打开“选择目录”并通过JavaScript交互写入文件系统?

[英]Can a Java applet open a “select directory” and write to a filesystem via JavaScript interaction?

I am looking for a way to save a couple of files created with JavaScript on the clients computer. 我正在寻找一种在客户端计算机上保存使用JavaScript创建的文件的方法。 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. 与JavaScript进行交互的某种方式,因为它知道要写入的文件名和内容。

Is this possible or should I just stop dreaming? 这有可能还是我应该停止做梦?

It is possible with a signed applet. 签名的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. 我可能会反过来做,让javascript代码将所有数据传递到applet,并且applet应该打开FileDialog并写入文件。

Calling javascript from java can be achieved via `JSObject , calling Java from Javascript is explained here 可以通过`JSObject来实现从Java调用javascript, 此处介绍从Javascript调用Java的方法。

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. 一种方法是使用签名的小程序,但是如果您的用户愿意升级到Java 1.6.0_10 +,则不需要签名的小程序。 Since 1.6.0_10 (the Next Generation Java Plug-In), JNLP API services are available to embedded applets. 从1.6.0_10(下一代Java插件)开始,JNLP API服务可用于嵌入式applet。 Here is a demo. 这是一个演示。 of the JNLP file services . JNLP文件服务

I see the other reply has already covered the applet/JS interaction. 我看到其他答复已经涵盖了applet / JS交互。

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"/> . 您可以打开“文件对话框”,但这将是Java的对话框,与用户按下标记<intput type"file"/> “浏览”按钮时打开的对话框不同。 Obviously signed applet can perform read and write operations with user's disk. 显然,已签名的applet可以对用户磁盘执行读写操作。

All applets (either signed or unsigned) may call javascript using JSObject . 所有小程序(已签名或未签名)都可以使用JSObject调用javascript。 You just have to say MAYSCRIPT="true" into applet tag. 您只需要在applet标签中说MAYSCRIPT =“ true”。

I think that you cannot open the browser's "file open" dialog applet. 我认为您无法打开浏览器的“文件打开”对话框小程序。 Actually you even cannot open it using regular javascript. 实际上,您甚至无法使用常规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). (我很高兴知道自己在这一点上是错的,但是我花了一些时间尝试这样做,但失败了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM