2

I have a download operation in my javascript code with angularjs $http provider.

$http({
    method: "POST",
    url: "http://localhost:28494/api/print",
    data: data,
    responseType:'arraybuffer'
}).then(
    function (response) {

        var file = new Blob([response.data], {type: 'application/pdf'});

        var objectUrl = URL.createObjectURL(file);

        window.open(objectUrl,'_blank');         
    }
);

This fires my browser popup. I do not want to this.

enter image description here

But I want to download directly. Do not show the popup.

ScrapCode
  • 2,049
  • 5
  • 21
  • 40
barteloma
  • 5,868
  • 11
  • 65
  • 150

2 Answers2

2

You should use window.location.assign(objectUrl);. This forces the window to open and display the url. In your case it will download the file.

Deividas Karzinauskas
  • 6,243
  • 2
  • 25
  • 26
0

Try this alternative approach.

window.location = item.objectUrl;

This will cause the browser to request a resource from the server, the response from server must include Content-Disposition:attachment;. It will cause the browser to show download dialog.

P.S. When you want to force the browser to show download prompt for some file (resource), you must include Content-Disposition:attachment; in the response.

ScrapCode
  • 2,049
  • 5
  • 21
  • 40