File Download

18 views
Skip to first unread message

D L H

unread,
Aug 19, 2008, 1:23:49 PM8/19/08
to Google Web Toolkit
Hello.

I want to create a gwt application that generates a file and allows
the client to download it. I was planning on generating the file with
a servlet, but how do i allow the client to download it without
navigating away from the application?

-DLH

georgopoul...@gmail.com

unread,
Aug 19, 2008, 3:42:04 PM8/19/08
to Google Web Toolkit
I use something like this:

String url = GWT.getModuleBaseURL() + "fileDownload?file=" +
URL.decode(path).toString();
Window.open(url, "_self", ""); // or windowName = _blank

Where 'fileDownload' is a servlet. In the HttpServletResponse I put:

response.setContentType("application/octet-stream");
response.setContentLength(Long.valueOf(
mRemoteFileIOService.getFileSize(pUserName, pUserPath,
fileName)).intValue());
response.setHeader("Content-disposition", "attachement; filename=
\""
+ getRealFileName(getFileName(fileName)) + "\";");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");

Mr. Len Weincier

unread,
Aug 19, 2008, 3:43:02 PM8/19/08
to Google-We...@googlegroups.com
Hi

You can do it in a servlet something like this

Regards,
Len

/**
* Allow us to retrieve static content
* Author: <a href="mailto:l...@guruhut.com">Len Weincier</a>
*/
public class DocServlet extends HttpServlet {

public void service(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ServletContext context = getServletContext();

File file = new
File(context.getRealPath(getPath(request.getRequestURI())));

// Return a 404 if we could not find the file
if (!file.exists() || !file.canRead()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
response.setContentType(mimeType);
response.setContentLength((int) file.length());

FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();

// Copy the contents of the file to the output stream in chunks
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();

D L H

unread,
Aug 20, 2008, 4:39:38 PM8/20/08
to Google Web Toolkit
Thanks! It works great.

I only have one more question. The file i'm generating with this
application is an xml-based format that I created specifically for
this application. What mime type would you recommend? i don't want
firefox (or any other browser) to give the option to open the file
with some xml reader.

Reinier Zwitserloot

unread,
Aug 20, 2008, 5:48:15 PM8/20/08
to Google Web Toolkit
application/xml+foobar

where foobar is something you come up with. And if that doesn't fly,
then you're stuck with just application/xml.

Reinier Zwitserloot

unread,
Aug 20, 2008, 9:15:00 PM8/20/08
to Google Web Toolkit
uhhh, you DONT want firefox to offer to open it with an external app.
application/octet-stream is what you're looking for.

Len Weincier

unread,
Aug 21, 2008, 2:47:31 AM8/21/08
to Google-We...@googlegroups.com
Hi

Well if its not really xml - i.e. just for your app then you can
create your own Text/MyCustomMimeType for it

Len

Reply all
Reply to author
Forward
0 new messages