1

I have the following client-side code:

var downloadUrl = buildRequestUrl(); 
window.open(downloadUrl, '_self');

That is working fine, but I need to pass some custom headers with the request, so I am using ajax, like this:

$.ajax(downloadUrl, {
   headers: { 'custom-header': 'custom-header-value'},
   type: 'GET',
   contentType: 'application/octet-stream'
}).done(function (data) {
   var blob = new Blob([data], { type: 'application/pdf' });
   var link = window.document.createElement('a');
   link.href = window.URL.createObjectURL(blob);
   link.download = '';
   link.click();
});

The file get downloaded, but the content is empty (3 blank pdf pages). What I am doing wrong ?

koryakinp
  • 3,989
  • 6
  • 26
  • 56
  • The `contentType` Ajax option in jQuery always marks the format you *send* data in. You are not sending any data in your GET request, setting the option there is useless. – Tomalak Apr 29 '18 at 07:58
  • Another post I found on the topic. http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/ – Tomalak Apr 29 '18 at 08:10

0 Answers0