JavaScript Blob窗口打开会触发浏览器弹出窗口

3

我在我的 JavaScript 代码中使用了 angularjs$http 提供者进行下载操作。

$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');         
    }
);

这会触发我的浏览器弹窗,我不想要这个。

enter image description here

但是我想直接下载,不要显示弹窗。


尝试 window.location = objectUrl - ScrapCode
你可以使用一个不可见的iframe吗?就像[这里][1]所示。 [1] https://dev59.com/Q2865IYBdhLWcg3wkfcW#3749395 - aljgom
1
@bookmarker,哪个解决方案帮到了你?请点赞/接受正确的解决方案。 - ScrapCode
@ArshadRehmani 没有人。 - barteloma
无法更改文件名?为什么要使用十六进制? - shinriyo
2个回答

4

0

试试这个替代方案。

window.location = item.objectUrl;

这将导致浏览器从服务器请求资源,服务器的响应必须包括Content-Disposition: attachment;。这将导致浏览器显示下载对话框。

P.S. 当你想要强制浏览器显示下载提示框以下载某个文件(资源)时,必须在响应中包含Content-Disposition: attachment;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接