Download file

Asked

Viewed 836 times

1

has how to download a file that is in binary format by js

var file = new Blob(['R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='], {type: 'image/png'});
              var fileURL = window.URL.createObjectURL(file);
              var a = document.createElement("a");
              a.href = fileURL;
              a.download =  "informe um nome";
              document.body.appendChild(a);
              a.click();
              $(window).on('focus', function(e) {
                $('a').remove();
              });

1 answer

3


You don’t need the Blob for that, you can do it directly with
a.href = 'data:image/png;base64,' + file;.

Example:

var file = 'R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==';
var a = document.createElement("a");
a.href = 'data:image/png;base64,' + file;
a.download = "informe um nome";
document.body.appendChild(a);
a.click();
$(window).on('focus', function(e) {
  $('a').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.