GWT and HttpServletResponse

508 views
Skip to first unread message

Akis

unread,
May 28, 2012, 11:26:57 AM5/28/12
to Google Web Toolkit
Hi guys. I've got a question for you:

i need to create a web app which, starting from an input string given
by the user through the browser, generates a file that has to be
downloaded by the user self.

My approach would be the following: the input string is sent by a RPC
to a server-side class that creates the file from the given string,
and then the file is sent to the client-side, ready to be downloaded.

Now, in "classic servlet way", i would use a HttpServetResponse to set
the header, the content type, and attach the file... how can i do this
with GWT?

Thank you mates!

kim young ill

unread,
May 28, 2012, 11:49:24 AM5/28/12
to google-we...@googlegroups.com
you can generate some metadata on serverside & send back to client (via rpc) &  let client generate the path to your file & then either redirect (with Window.assign(url) or let user click to the generated link to get the file.
i dont think you can get binary input stream from client to get the file directly with rpc

hth


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


Thomas Broyer

unread,
May 28, 2012, 11:51:53 AM5/28/12
to google-we...@googlegroups.com
"With GWT", yes; but not with GWT-RPC (just like you couldn't do it with SOAP WebServices // granted, you can use a SOAP extension to attach files to SOAP messages, but still).

Either you use GWT-RPC and then the server has to generate the file on a temporary storage (could be in-memory / in-session) and sends an URL back to the client, and then the client can download that file at the given URL.
Or you use a FormPanel to communicate with the server.

The first method works best if the generated file could be displayed in the browser (right away or using a plugin; such as plain text, PDF, MS Word, etc.), so I'd recommend it over the FormPanel, despite being more complex (temporary storage has to be cleaned-up, otherwise it's not temporary)

Joseph Lust

unread,
May 28, 2012, 1:24:58 PM5/28/12
to google-we...@googlegroups.com
Akis,

If you don't have to support old browsers, and if the file is not that big (say 100K) you could encode it as a dataurl on the server, and pass that back to the UI. Then you could serve the client that file (in encoded dataurl form).

I've not done the above, but I think it would work as a way to send a file through GWT RPC. Basically it is using the same mechanisms as a DataResource in ClientBundle (see pdf example).

There is a method you could adapt for this in the phonegap code, FileUtils.readAsDataUrl(). Of course the base64 encoding will make you at least double the bandwidth over the binary form of the file.

Sincerely,
Joseph


Akis

unread,
May 29, 2012, 5:46:05 AM5/29/12
to Google Web Toolkit
The generated file has to be automatically downloaded on button
pressing. Maybe, in future, the generated file could also be a zip
file, containing several resources generated from input client-side
data, so it could also be a large file

kim young ill

unread,
May 29, 2012, 6:07:28 PM5/29/12
to google-we...@googlegroups.com

i think the most suitable solution is using a formpanel as thomas said, so u can stream the back the data to client right away  from your servlet


 

Laszlo Fenyvesi

unread,
May 28, 2012, 11:33:54 AM5/28/12
to google-we...@googlegroups.com
You need to make an other ajax call after the file is created.
From the RPC call send back to the client the url pointing to the newly
created file, and:

static native void downloadFile(String url) /*-{
$wnd.open(url,'Download');
}-*/;

Ml,
fenyoapa

----- Eredeti �zenet -----
From: Akis
Sent: Monday, May 28, 2012 5:26 PM
To: Google Web Toolkit
Subject: GWT and HttpServletResponse

Joseph Lust

unread,
Jun 1, 2012, 3:22:39 PM6/1/12
to google-we...@googlegroups.com
A trick I've used in this sort of application is to just POST a <form> to the file generating servlet with target="_blank". This will open a new window, but since the window is a file download, it just launches the file download instead of a new window. This combines the step of (1) Getting the download URL passed back to application (2) Opening said URL in a different window.

Sincerely,
Joseph
Reply all
Reply to author
Forward
0 new messages