How to download a file from server without browser opening new window?

6,296 views
Skip to first unread message

membersound

unread,
Feb 1, 2013, 10:57:06 AM2/1/13
to google-we...@googlegroups.com
Hi,

I created a servlet that provides a downloadable file (from String content) by writing to the ServletOutputStream.
On the client side, I trigger the file download by an Anchor with:

Window.open(GWT.getModuleBaseURL() + "MyServlet", "_blank", "");

It works fine, BUT it seems to open a new browser window, which is somehow directly closed. After this the file download dialog is show.
How can I prevent this flickering of a new browser window? Can't I achieve this somehow inline?

Thomas Broyer

unread,
Feb 1, 2013, 11:13:06 AM2/1/13
to google-we...@googlegroups.com
Can't you just link to your servlet?

<a href="{0}">Download</a> (where {0} is replaced with your URL)

or new Anchor("Download", GWT.getModuleBaseURL() + "MyServlet")

membersound

unread,
Feb 1, 2013, 11:18:34 AM2/1/13
to google-we...@googlegroups.com
I'm using ui:binder. How should I provide href for the anchor as GWT.getModuleBaseURL()?

Andrea Boscolo

unread,
Feb 2, 2013, 8:26:58 AM2/2/13
to google-we...@googlegroups.com
Define your anchor in uibinder and use @UiField(provided = true) in the java file. Then instantiate it with that url before the binding call.

Kody

unread,
Feb 2, 2013, 10:35:39 AM2/2/13
to google-we...@googlegroups.com
Ah that's a great thing, thanks a lot!

membersound

unread,
Feb 3, 2013, 4:24:01 PM2/3/13
to google-we...@googlegroups.com
Hm just another issue: I also want the servlet url to have some query parmeters like MyServlet?param=content.

But the content is dynamic, especially not available on construction of the Anchor.
How can I pass this content on click into the targetLink of the Anchor before it gets executed?

The following at least does not work. Somehow setting the new target if the anchor is clicked does not have any impact. Why?

@UiHanlder("anchor")
void onAnchor(ClickEvent event) {
   anchor.setTarget(servletUrl + content);
}

private servletUrl = GWT.getModuleBaseURL() + "MyServlet" + "?param=";

Abraham Lin

unread,
Feb 3, 2013, 5:02:01 PM2/3/13
to google-we...@googlegroups.com

Thad

unread,
Feb 6, 2013, 10:41:28 AM2/6/13
to google-we...@googlegroups.com
I have a number of places where the user can download a servlet generated file. I handle this generically with several steps

1) In my app's HTML, I put an IFRAME

    <!-- Frame for downloading files without opening a new window.   -->
    <iframe src="javascript:''" id="__gwt_downloadFrame" style="width:0;height:0;border:0"></iframe>

2) In my app's EntryPoint, I declare

  private static final String DOWNLOAD_IFRAME = "__gwt_downloadFrame";
  private static Frame downloadFrame;
...

Wrap the IFRAME:

  @Override
  public void onModuleLoad() {
    downloadFrame = Frame.wrap(Document.get().getElementById(DOWNLOAD_IFRAME));
...

Provide method for downloading

  public static void downloadURL(String url) {
    downloadFrame.setUrl(url);
  }

3) Whenever a download is ready, say in the onSuccess() of an RPC call

  MyApp.downloadURL(url);

Patrick Tucker

unread,
Feb 7, 2013, 9:17:30 AM2/7/13
to google-we...@googlegroups.com
You realize you are telling it to open in a new, "_blank", window?  If you tell it to open in "_self" you won't see the extra window open.  This brings on another issue though, if the browser decides the file can be opened internally, your user will end up losing their current state, assuming the app is not setup to reload correctly.

On Friday, February 1, 2013 10:57:06 AM UTC-5, membersound wrote:
Reply all
Reply to author
Forward
0 new messages