我需要下载点击链接上的文件,window.open 似乎是一个不错的选择。所需要做的就是点击包含文件名作为参数的网址。
现在有了图中的登录身份验证,如何将授权标头添加到 window.open(url,'_blank')
2024.09 之前,使用 window.open 时无法添加 header。我面临着同样的问题。对我来说,我将 window.open 替换为这样的标题:
let accessToken = getAccessToken();
let url = "/the/pdf/url";
fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + accessToken,
},
})
.then((response) => response.blob())
.then((blob) => {
var _url = window.URL.createObjectURL(blob);
window.open(_url, "_blank")!.focus();
})
.catch((err) => {
console.log(err);
});