调用后端接口下载pdf文件
vue3项目以文件流blob形式下载pdf
//调用接口
export function getAppendixByName(data = {}, params) {
return service({
url: "",
method: "get",
responseType: 'blob',
params: params,
data,
})
}
getAppendixByName(
{},
{
resource: route.params.attachmentUrl,
}
).then((res) => {
const { data, headers } = res;
let blob = new Blob([data], { type: headers["content-type"] });
let url = window.URL.createObjectURL(blob);
let link = document.createElement("a");
link.href = url;
link.style.display = "none";
link.setAttribute(
"download",
route.params.attachmentUrl.substring(
route.params.attachmentUrl.lastIndexOf("/") + 1
)
);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
});
出现的问题:oppo手机下载提示”只能从http或https网址下载“
暂时解决方法:安卓系统使用a标签下载,不走接口;ios系统需要调用接口进行下载,否则只是跳转页面,不会进行下载。