Download file with GWT

2,429 views
Skip to first unread message

hond...@gmail.com

unread,
Sep 10, 2008, 8:03:50 AM9/10/08
to Google Web Toolkit
Hello,

I want to download a file when in click on a button. I try this code
found here

(http://groups.google.com/group/Google-Web-Toolkit/browse_thread/
thread/c8b8d2adc4128c6c/aa645788f6a74a1c)

but it doesn't work.

Please someone could help me ?

Thank you.

Regards.

obesga

unread,
Sep 10, 2008, 12:23:35 PM9/10/08
to Google Web Toolkit
What are you trying on client-side ?
What's exactly the error you have ?

Oskar

Simon Rydberg

unread,
Sep 11, 2008, 4:34:55 AM9/11/08
to Google Web Toolkit
I'm having the same problem.. I have a servlet that I make a POST ( I
can't do a GET request because I send a lot of data to the servlet )
request with the RequestBuilder to the servlet and it recives all the
data, creates, in my case an excel file and then the client recives
the responce in a RequestCallback. And then nothing happens, except
that the status of the responce is OK with code 200.. I want the user
to see the download file form when the responce comes.

I have searched this group and via google with an answer to my
problem. Found some answers when you are making a GET-request, but
nothing when you make a POST request...

Manik Chand

unread,
Sep 11, 2008, 4:53:27 AM9/11/08
to Google-We...@googlegroups.com
Hi,

I have a piece of code please check it. 

I tried this and successful in receiving file.
My file type is ".txt".

//----------------------------------------

//Write A servlet class extending HttpServerlet

inside doGet method write this.

resp.setContentType("application/csv");
resp.setHeader("Content-disposition", "attachment; filename=" + "Report.txt");

ServletOutputStream outputStream = resp.getOutputStream();

for (int i = 0; i < buff.length(); ++i) {
outputStream.write(buff.charAt(i));
}
outputStream.close();



At client side:---

String baseURL = GWT.getModuleBaseURL();
String url = baseURL + "/VersionDetailServlet.do";
Window.open(url, "", "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=no,toolbar=true, width=" + Window.getClientWidth() + ",height=" + Window.getClientHeight());

It will prompt a dialog to save file.
--
Manik Chand
Software Engineer
Exact Software Pvt. Ltd.
403, 4th Floor, Accord Complex,
Station Road,
Goregaon(East),
Mumbai - 400063
Cell No. : +91 9220984430
www.exact-solutions.com

Folke

unread,
Sep 11, 2008, 5:08:48 AM9/11/08
to Google Web Toolkit
Plan A: Storing the file on the server.

1. Submit the data to the server
2. The server converts and stores the Excel data (temporarily) and
assigns an ID
3. ID is sent back to the client
4. Open a new window or use an iframe with the download URL containing
the ID as GET parameter
5. Server sends the Excel data with MIME type "application/octet-
stream".
6. "Save as ..." dialog opens.
7. ???
8. Profit!

Plan B: IFrame with form submit
1. Create an invisible IFrame containing a form with your data in a
hidden field
2. Submit the form via Javascript.
3. (magick)
4. "Save as ..." dialog opens.
5. ???
6. Profit!

Reinier Zwitserloot

unread,
Sep 11, 2008, 7:45:50 AM9/11/08
to Google Web Toolkit
1. The server has to send the file with a data type of application/
octet-stream.
2. You can't use an AJAX call (RequestBuilder / HttpRequest / GWT-
RPC), those will never generate a save as box on the user's client.
Use:

1. A link that a user clicks on.
2. open a new window using Window.open
3. redirect the current page to your excel page. I'm not sure if
there's a GWT way, but with JSNI it's a matter of "window.location =
"http://whatever"';
4. Like #3, but do this in an iframe you just created, so that the
user doesn't lose the GWT page.

Jason Essington

unread,
Sep 11, 2008, 11:45:40 AM9/11/08
to Google-We...@googlegroups.com
Number 1 is actually very important. If you are going to use
Window.open it has to be done as a direct result of a user action,
otherwise popup blockers will prevent the window from being opened.
This means that you can't place the Window.open command in a callback
or any kind of deferred call.

-jason

Reply all
Reply to author
Forward
0 new messages