Button => download file ?

2,211 views
Skip to first unread message

Peter

unread,
Apr 7, 2008, 3:42:30 PM4/7/08
to Google Web Toolkit
Hi,

I want to create a button to download a file in my gwt app like when
attachments are downloaded from within gmail: a click on the button
leads to a "save file as..." dialog. Is there some straight way to do
this kind of thing with gwt?

Thanks

Peter

kilkenny

unread,
Apr 8, 2008, 9:29:52 AM4/8/08
to Google Web Toolkit
Hi Peter

The easiest way is probably to use a hyperlink that points to the file
to download or to a servlet which streams your file to the client. If
a button is required, you could use a ClickListener to open the
download URL in a separate window. See the code below:

---
Button b = new Button("Download", new ClickListener() {

public void onClick(Widget sender) {
Window.open(GWT.getModuleBaseURL() +"/downloadServlet?fileId=123",
"123", "");
}

});
---

The download servlet needs so set the correct headers otherwise the
"save file as..." will not be displayed.

Regards, Adrian

David Given

unread,
Apr 8, 2008, 12:48:00 PM4/8/08
to Google-We...@googlegroups.com
kilkenny wrote:
[...]

> The easiest way is probably to use a hyperlink that points to the file
> to download or to a servlet which streams your file to the client.

If you want to do it all from withing GWT there's a harder way, which is
considerably more evil: create a link to a URL in the data: scheme.

http://en.wikipedia.org/wiki/Data:_URI_scheme

For example, the URL data:text/plain,Fnord! will produce a link to a
text file containing the contents "Fnord!". You can do any MIME type
this way, and you can provide binary data base64 encoded.

Of course, this does mean that you have to fit your entire data into a
URL, and IE6 is limited to about 2kB for URL sizes, so it may not be
suitable for your purposes.

--
David Given
d...@cowlark.com

Martin

unread,
Apr 8, 2008, 1:06:43 PM4/8/08
to Google Web Toolkit
Hi

>         public void onClick(Widget sender) {
>                 Window.open(GWT.getModuleBaseURL() +"/downloadServlet?fileId=123",
> "123", "");
>         }

I am using something like:

public static native void gotoUrl(String url) /*-{
$wnd.location = url;
}-*/;

ml

unread,
Apr 17, 2008, 12:43:35 PM4/17/08
to Google Web Toolkit
This worked for me with an excel file.

com.google.gwt.user.client.Window.open("http://www.urltofile.com/
file.xls", "_self", "");

You may need to add to the response headers:
Content-disposition: attachment; filename=xxx.xxx

Carl Scott

unread,
Apr 17, 2008, 11:14:05 PM4/17/08
to Google Web Toolkit
Files that your browser won't attempt to handle natively will default
to using the download dialog. If your browser has any means to attempt
to render a file (html, xml, pdf, images, etc), it will do so unless
you provide the appropriate headers, which is why you need the servlet
or something like it as an intermediary between fetching the resource
and returning it to the client. Just google content-disposition
headers to get specifics on headers for files and file types.

--
Carl Scott
Software Developer, Solertium Corporation
Reply all
Reply to author
Forward
0 new messages