Download file [closed]

1

you can 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();
              });
    
asked by anonymous 10.08.2017 / 02:57

1 answer

3

You do not need Blob for this, you can do 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>
    
10.08.2017 / 08:06