0

I have a webpage encoding a local file into Base64, and would like to give the user an option to save the result - now residing in a html element - without going back to the server. I could post the data to a back-end webpage, save as file, and offer a <a href= .. but I would much appreciate the option of fulfilling this on the client side. The file can easily be too big for a post.. Could I write som javascript code to initiate a save dialog, and push the file content back to the user if he chooses to accept the file ?

thanks for any help ! Kind regards Ketil Duna

Ketil Duna
  • 1
  • 1
  • 1

1 Answers1

1

You could do it like this:

var file = //your Base64 encoded file
var url = 'data:application/octet-stream;base64,' + file;
window.open(url);

You should adapt the code so the mime type matches the content you are serving (application/pdf...)

To pass a file name you will have to use a anchor tag like follows :

<a href="data:application/octet-stream;base64,/*YOUR FILE DATA*/" download="your filename">

but this is not crossbrowser. See http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download

Armel Larcier
  • 15,747
  • 7
  • 68
  • 89