1.问题描述:
调下载附件接口时,若接口无法提供url,以文件流的形式直接返回时,如:
2. 解决方案:
请求接口时传入:responseType: ‘arraybuffer’
/**
* api.js
* @param { 附件id }
* @returns
*/
export const getdownloadFile = (id) => {
let downloadFile = request({
url: '/AppNoticeAction!downloadFile.do',
method: 'post',
responseType: 'arraybuffer', // 重要的一行
data: {
"id": id,
},
});
return downloadFile
}
此时res
vue组件如下:
<a :href = "qrBase64" :download = "fileName" class = "getDownLoad">
下载附件
</a>
data() {
return {
qrBase64 : "", // 接口返回的图片转成的base64
fileName : "", // 下载时显示的附件名称
};
},
//附件接口回调
getdownloadFile(this.getFileId).then((res) => {
this.isImg = true;
this.qrBase64 =
"data:image/png;base64," +
btoa(
new Uint8Array(res.data).reduce(
(data, byte) => data + String.fromCharCode(byte),
""
)
);
console.log("qrBase64", this.qrBase64); // 转成的qrBase64
});