3

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,109
  • 5
  • 24
  • 44
barteloma
  • 6,403
  • 14
  • 79
  • 173

2 Answers2

4

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
  • 6,437
  • 2
  • 26
  • 27
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,109
  • 5
  • 24
  • 44