DG.
DG.

Reputation: 553

How to download a file using GWT client?

What is the best way to download a pdf file using GWT client ? Should I invoke a normal servlet to do that ? or is there a different preferred approach to handle this problem ?

I am new to GWT, so if some sample code would be of great help.

Thanks Deep

Upvotes: 8

Views: 13214

Answers (3)

Italo Borssatto
Italo Borssatto

Reputation: 15709

You can implement a Servlet download the file OR you can do that using Data URIs:

  1. Make your GWT RPC method return the file content or the data to generate the file.
  2. On the client side, format a Data URI with the file content received or generate the data content.
  3. Use Window.open to open a file save dialog passing the formatted DataURI.

Take a look at this reference, to understand the Data URI usage:

Export to csv in jQuery

Upvotes: 1

Sagar Varpe
Sagar Varpe

Reputation: 3599

the best way is to navigate your browser to that file

on download button add click handler:

Button downloadButton = new Button("Download");
downloadButton.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        Window.open("url_of_file", "download File", "");            
    }
});

Upvotes: 0

cupakob
cupakob

Reputation: 8551

Try it with GET...

Window.open(GWT.getHostPageBaseURL() + "FileRepository/doDownload?docId=" + dokument.getId(), "", "");

Upvotes: 4

Related Questions