Is there any way to implement write/read file with gwt on client-side?
I tried with java.io.File, java.io.Writer ... I couldn't succeed.
thx in advance!
Update: Please see my own answer for a solution
No, you can't write to files on the client-side. GWT only binds a subset of the Java language. Any file IO would need to happen on the server side through RPCs or some kind of web service.
It's possible with HTML5 in some modern browsers. Try lib-gwt-file. This library can read files from client computer and even supports DND. To see it in action follow this link.
More information on HTML5 FileAPI you can find in the specification.
To download a file from browser memory to the client computer you can use Data URI. Example is here. But this feature is supported by Google Chrome only. Also take a look at the following helpful function. It runs download without reloading current page:
public static native void setWindowHref(String url)/*-{
$wnd.location.href = url;
}-*/;
Another semi-crossbrowser way is Downloadify. It's based on flash. Check this example.
Recently, I've stumbled upon a library called client-io.
A simple library that brings the Flash File API to regular web apps
through GWT. ClientIO will help you offload some of the file
generation functionalities to the client, saving resources and heavy
computation to the server. Working Demo - http://ahome-it.github.io/ahome-client-io/
In GWT the classes in the client folder are only compiled into javascript hence it is not possible to use
java.io
since GWT does not provide compilation of the package
java.io
Hence you have to write text file through RPC only.
Related
I want to know what is the best approach to take to build a GWT library into a JavaScript library. When this GWT library to be compiled into Java script does not have any GWT server side component in it.
Just plain front-end components. Including only things like AJAX calls, etc.
In Java the GWT library is used like this:
TheGWTLibrary api = new TheGWTLibrary();
api.setServer("http://www.somewhere/api");
api.post(stuff, new Callback(){
void success(){
}
void fail(){
}
});
api.get(new Callback(){
void success(){
}
void fail(){
}
});
Such that the GWT library project would be usable in any HTML project without making it a GWT app.
If this is possible where will the dependencies of this GWT library be compiled to? Will it be included in a one single JavaScript (js) file?
[EDIT 2016]
The way to go with new GWT (2.8.0) is using JsInterop, read the documentation API. Note that it's still in 2.8.0-SNAPSHOT but will be released in few weeks. Also, JsInterop in 2.7.0 has been deprecated and removed in 2.8, so don't use it.
[END EDIT]
Apart from writing your JSI method, there are two easy ways to deal with this.
1.- GwtExporter
You might be interested on reading this article I wrote some years ago:
https://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en
It uses gwt-exporter, and you can take a look to a couple of projects using this approach:
JsUpload wich is a port of gwtUpload:
https://code.google.com/p/gwtupload/wiki/JsUpload_Documentation
https://github.com/manolo/gwtupload/tree/master/jsupload
GwtChismes is a very old library useless right now and not maintained anymore, but it is exported as well
https://code.google.com/p/gwtchismes/wiki/JsChismes_Documentation
https://code.google.com/p/gwtchismes/source/browse/#svn%2Ftrunk%2FGWTChismes%2Fsrc%2Fjschismes
I also did some experiments exporting gwtquery to js (jsQuery):
https://code.google.com/p/gwtquery/wiki/JsQuery
Chronoscope was another gwt library using this approach
https://code.google.com/p/gwt-chronoscope/wiki/JavaScriptAPI
2.- JsInterop
If you want to play with new stuff, you might read about JsInterop a beta feature in GWT-2.7 which will be fully functional in GWT-3.0.
It allows you to export java classes to JS. You have to use the -XjsInteropMode JS and some annotations.
There is no so much documentation right now, but there is a document explaining the API, and an interesting presentation.
https://docs.google.com/document/d/1tir74SB-ZWrs-gQ8w-lOEV3oMY6u6lF2MmNivDEihZ4/edit
http://gokdogan.appspot.com/gwtcreate2013/#1
It sounds like you're looking for something like GWT Exporter. It allows you to export a GWT library as a publicly accessible javascript API.
It looks like you'll still need to compile the GWT library as a web application to use it, but if you don't have any server-side code, it should be as simple as compiling your API, copying the war folder and adding a reference to war/[yourapp].nocache.js.
If that's not what you're looking for, you can check out this question for other options.
I have a problem with attaching a file to a specific item using Java API. I know it should be possible as this functionality described here in the Podio documentation https://developers.podio.com/doc/files/attach-file-22518 and examples for PHP and Ruby are given. However I cannot find such method in the podio java library. I could find in FileAPI just methods that provide uploading files, but not attaching them to specific objects as described in documentation.
I use Podio APi version 0.7.1
Any ideas how it should be done in Java?
Podio uses a REST-Style API. You send standard http-request, and you get back json-formatted data. So you can do it all without a special library for your programming language.
If there is no predefined java class for you, you can just do the call yourself. In the end it is just a HTTP-call.
From the ruby implemention, I see that you attach the file as multipart/form-data,
so it is the same a browser would do it. There should be http-handling java classes to help you.
You also need to add the information from the API-Page, like the POST-Parameters and of course the url. The most difficult part is probably the authentication headers, but you need to solve this problem only once.
I want to make a Chrome plugin that checks if mencoder.exe is present on the clients system. And if so, convert files with it.
Is that possible?
If not, can I make something like that in a Java applet or something? I'm open to suggestions!
It is possible with java applets, but you also need to implement a permission policy into the program. Here are two tutorials you should take a look at:
Java applets:
http://docs.oracle.com/javase/tutorial/deployment/applet/
Implementing Policy:
http://docs.oracle.com/javase/tutorial/security/userperm/policy.html
You can do it using java applet (like Laerte said), but implementing a policy is not the only option. May be it is easier digital signing (info) your applet with respective attributes in Manifest.mf (here and here) and wrapping your SO access source lines with this. In this way, you can distribute your applet without modifying client systems.
I would just like to ask if there is a way to upload files in GWT WITHOUT using the Google App Engine and Apache Commons archives? I've been searching for ways to upload files in GWT but all of the solutions I find all make use of these two. I would just like to know if there is a way, because our app won't work if we use GAE and Apache Commons... Thank you very much!
Yes, the simplest would be (assuming you are saving files to Blobstore):
On GWT side use FileUpload. Here is an example on how to use it.
On GAE side use BlobStore upload handler.
The other option would be to use gwt-upload with GAE upload handler.
GWT does not have such feature. You need to use some existing library or handle multipart form submits by yourself (FileUpload class). There is for example a gwtupload library which I have used and it worked pretty fine (but it is based on commons-upload AFAIR). There are other libs for sure.
http://code.google.com/p/gwtupload/
I am helping someone out with a javascript-based web app (even though I know next to nothing about web development) and we are unsure about the best way to implement a feature we'd like to have.
Basically, the user will be using our tool to view all kinds of boring data in tables, columns, etc. via javascript. We want to implement a feature where the user can click a button or link that then allows the user to download the displayed data in a .doc file.
Our basic idea so far is something like:
call a Java function on the server with the desired data passed in as a String when the link is clicked
generate the .doc file on the server
automatically "open" a link to the file in the client's browser to initiate the download
Is this possible? If so, is it feasible? Or, can you recommend a better solution?
edit: the data does not reside on the server; rather, it is queried from a SQL database
Yep, its possible. Your saviour is the Apache POI library. Its HWPF library will help you generate Microsoft word files using java. The rest is just clever use of HTTP.
Your basic idea sounds a bit Rube-Goldbergesque.
Is the data you want in the document present on the server? If so, then all you need to do is display a plain HTML link with GET parameters that describes the data (i.e. data for customer X from date A to date B). The link will be handled on the server by a Servlet that gets the data and produces the .DOC file as its output to be downloaded by the browser - a very simple one-step process that doesn't even involve any JavaScript.
Passing large amount data as GET/POST around might not be the best idea. You could just pass in the same parameters you used to generate the HTML page earlier. You don't even need to use 3rd party library to generate DOC. You could just generate a plain old HTML file with DOC extension and Word will be happy to open it.
Sounds like Docmosis Java library could help - check out theonline demo since shows it something similar to what you're asking - generating a real doc file from a web site based on selections in the web page. Docmosis can query from databases and run pretty much anywhere.