13

my HTML5 web application receives a file as binary data via a websocket. Is there any way to initiate a file download dialog (via javascript) to save the received data to the users disk?

Thank you!

Herbert Stiftler
  • 131
  • 1
  • 1
  • 3

2 Answers2

5

You need to build a dataUri and open a new window with this. Specific mimetypes can trigger the browser's automatic save as dialog.

Example data uri: "data:application/octet-stream,base64" + base64binary

Peter Aron Zentai
  • 11,482
  • 5
  • 41
  • 71
  • +1 Good thought, it even seems to work: http://jsbin.com/acohaz There are [some browser compatibility issues](http://caniuse.com/#feat=datauri) -- not least that not even IE9 supports using `data:` URIs this way yet -- but it's the only game in town if you really need to have the data held client-side and then initiate the download window. – T.J. Crowder Mar 26 '12 at 11:49
2

Not yet, no. The new File API currently only covers reading. Update: See Peter's answer if you only need to do this on cutting-edge (and to date, non-IE) browsers, where you can do it with the data: URI scheme.

The usual way to do this would be to receive the file via HTTP into a hidden iframe using Content-Disposition: attachment to trigger the file download dialog, rather than receiving it via a web socket.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875