遇到下载需求,后端把文件转成了base64数据返到前端,下面描述下拿到这个base64数据转成blob格式在前端下载的方法:
downloadAttachment({ attachmentId: id })
.then(({ data }) => {
proxy.$modal.closeLoading();
const raw = window.atob(data.content);
const rawLength = raw.length;
const uInt8Array = new Uint8Array(rawLength);
for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
const blob = new Blob([uInt8Array], { type: data.contentType });
const elink = document.createElement("a");
elink.download = data.attachName;
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
})
.catch(() => {
proxy.$modal.closeLoading();
});