PDA

View Full Version : Download File when clicking a ToolBar item.



villemustonen
4 Sep 2008, 7:43 AM
Hi,

I have a ToolbarItem that has a SelectionListener. How do I make clicking this ToolbarItem download a file from a given url? Seems like a simple enough thing to do but I cannot for the life of me figure it out.

--Ville Mustonen

gslender
4 Sep 2008, 1:35 PM
in GWT create an innerframe and set the url of the frame to the file you wish to download...

yopiyop
5 Sep 2008, 12:18 AM
client code :


t = new TextToolItem("Excel", "file-extension-xls");
t.addSelectionListener(new SelectionListener<ComponentEvent>() {
public void componentSelected(ComponentEvent ce) {
Window.Location.assign(
GWT.getModuleBaseURL() +"download?monparam=bla"
+"&id="+id
);
}
});
serveur code :


response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "Attachment;Filename=\""+nom+"."+"xls" + "\"");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "must-revalidate");

....send outputstream....

villemustonen
5 Sep 2008, 12:33 AM
Thank you both for helping, I got it working, but suppose the browser is set to play the downloaded file in the browser itself (it's audio), so then it would not pop up the file dialog. Is there (or is one planned) a EXTish way to do this? :)

villemustonen
11 Sep 2008, 3:03 AM
Actually, the way I want this to work is that when the user clicks the toolbar button, a request is sent from client->server->DB to get the download URL for the specified item (type audio/mpeg). So by clicking the button the user sends a request to get the url, and the appropriate action must be handled in the callback. I just can't get it working so that when the response arrives it would automatically pop-up a download dialog. Is it possible?

EDIT: I forgot to mention, that getting the URL is already implemented, I'm only looking for a simple way to pop-up a download dialog after the response arrives.

johnleroux
21 Sep 2008, 8:42 PM
After a whole night of searching (Pascal is so much easier :)) I use in my javascript location.href = 'url-to-file'
I then get the download or open dialog from the browser.
It works in FF3 and IE7. I am sure there is an easier and better way but this works.

joygto
16 Feb 2009, 7:02 PM
i can download file by code in HttpServlet ,but i need send much parameter info to server side,so i send a java serializable object to server side (by rpc call) ,so i trying follow code in rpc impl for download file :

HttpServletResponse response = this.getThreadLocalResponse();
response.setContentType("application/ms-excel");
response.setHeader("Content-Disposition", "inline; filename=" + "xhye");

java.io.PrintWriter out = response.getWriter();
out.write("oh my god");
out.flush();
out.close();

but occur a error and the error info is :

Exception while dispatching incoming RPC call
java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
...
then,what should i do to solve this problem? thanks.