【发布时间】:2019-08-29 11:55:14
【问题描述】:
我有一个从 react 调用的 api。它返回一个pdf文件。当我单击链接作为href时,我可以下载pdf。 现在,我调用的是一个函数,而不是一个href,单击并从该函数中调用api。但我无法下载该文件。 这就是我正在做的:
fetch(<url>, {
method: "GET",
headers: {
Accept: "application/pdf",
"Content-Type": "application/pdf",
},
}).then(response => response.blob())
.then(response => {
var blob=response
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
window.open(base64data);
}
})
.catch(error => {
console.error(error);
});
我无法下载任何文件。 api(用 kotlin 编写)返回一个字节数组。 此外,如果 api 抛出异常而不是返回 bytearray,我需要显示一个弹出窗口, 对此有什么想法吗?
【问题讨论】: