Reputation: 683
Hy there, I am fairly new and inexperienced with those fancy web technologies, so sorry for the probably dumb question. I have a GWT app that
So far everything works as expected, my only problem now is that i can't for the life of mine find a way to properly trigger the download of the file.
The download itself is handled by a servlet, this topic is very good covered in various questions here. What is left out in all questions i found thus far is: "How do i 'call/trigger/whatever' that servlet without loosing the GWT state or without opening an obstrusive new window?".
The following snippets work in theory, but are not a valid option in my opinion.
String url = GWT.getHostPageBaseURL() + MyExport.SERVLET_URL_PATTERN + "?" + MyExport.FILENAME + "=" + result.getFileName();
// After this call my GWT state is lost
Window.Location.assign(url);
// obstrusive pop-up that is blocked by most browsers anyway
Window.open(url, "NameOfNewWindow", "resizable,scrollbars,status");
It might be of interest that i try to set these calls from within my presenter class. Sorry in advance if i didn't find the correct question and this is a duplicate, i will continue searching and post everything i find.
Upvotes: 1
Views: 1476
Reputation: 683
The IFrame approach works quite nicely for me. In some utility class on the client side you can provide a simple static "triggerDownload(String url)"
The IFrame:
<iframe src="javascript:''" id="__gwt_downloadFrame" tabIndex='-1' style="position: absolute; width: 0; height: 0; border: 0; display: none;"></iframe>
The code that triggers the download:
import com.google.gwt.dom.client.Document;
import com.google.gwt.user.client.ui.Frame;
public class Utils
private static Frame downloadFrame;
public static void triggerDownload(final String url) {
if (url == null) {
throw new IllegalArgumentException("URL must not be null");
} // if
if (downloadFrame == null) {
downloadFrame = Frame.wrap(Document.get().getElementById("__gwt_downloadFrame"));
} // if
downloadFrame.setUrl(url);
} // triggerDownload()
} // class Utils
Upvotes: 2
Reputation: 626
I use code similar to your Window.open code in an application that downloads a server-side generated pdf, very similar to what you are trying to do. It does not result in a popup, since the content type of the file that the servlet streams is set correctly:
// Client side code in a click handler that triggers the download
Window.open("PromoPriceList?fileKey=" + itemKey, "_blank", "");
// In the servlet that is called, resp is the HttpServletResponse
resp.setContentType("application/pdf");
resp.setDateHeader("Expires",0);
resp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
resp.setHeader("Pragma", "public");
resp.setHeader("Content-Disposition", "inline; filename=" + /* name for client */);
resp.setContentLength(/* size of file */);
// Stream the content with resp.getOutputStream().write(...)
Upvotes: 0