0

I have Base64 files that I am trying to have the user download. I do not need these or want these to display in the browser. I need these to download. The data seems to be coming back fine, but only certain types of files are behaving.

I am grabbing down the data in an ajax call and then checking to see if there is any data.

$('button').on('click', function(){
   ...ajax call
  if (data) {
          var encode = 'data:image/' + data.dataTypeCode + ';base64,'
          var image = encode+data.data;
          window.open(image, '_blank');
      }
  })

This is only opening the word, excel, gif, mpg, tif and pdf files. This is not opening the png, jpg, mp3 files which I find odd.

zazvorniki
  • 3,512
  • 21
  • 74
  • 122

1 Answers1

-1

You cannot force a user to automatically download a file simply due to the file represented as a data URI or Blob URL being opened in a window. You can offer a file to be downloaded.

const a = document.createElement("a");
a.download = "fileName";
a.href = /* data URI, Blob URL*/;
document.body.appendChild(a);
a.click();
guest271314
  • 1
  • 15
  • 104
  • 177
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/163354/discussion-on-answer-by-guest271314-browser-only-downloading-certain-base64-file). – Brad Larson Jan 17 '18 at 20:18