1

I have user information presented in the UI, where the use can download it as a file.When the user clicks on download, i want to write the information that's present in the client side to a file and make it downloadable.So all the download features should be tight to client, without any server intervention.I came across this post,

1) How can a user download a file in client side (Google Web Toolkit)

But didn't help much.

Thanks.

Community
  • 1
  • 1
Karthik207
  • 493
  • 9
  • 27

2 Answers2

1

You can't write to a file, but you can create a blob (out of strings or typed arrays) and obtain a URL to that blob to make it downloadable (just put that URL as the href of a link, possibly adding a download attribute to force download).

Currently, GWT has some support for typed arrays (only actually useful if you need binary rather than text), but not for blobs, so you'll have to use JSNI (or possibly GWT Elemental).

For browser compatibility, see http://caniuse.com/#feat=blobbuilder and http://caniuse.com/#feat=blobbuilder, http://caniuse.com/#feat=bloburls and http://caniuse.com/#feat=download

Note: there are other solutions involving Flash or other pugins but browser compatibility won't be really better (you'll reach older browsers, but might lose newer ones, particularly as people switch more and more to a "click to play" pattern for plugins).

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
0

If the file isn't too large you can use the data URI scheme as

button.setAttribute("download", "filename.ext");    
String url = "data:Application/octet-stream;base64," + dataAsBase64;
button.setHref(url);

I made a fiddle at http://jsfiddle.net/cn1ckawL/

liftarn
  • 429
  • 3
  • 21