// 下载附件
Download() {
console.log(this.formFather.compressFileId, "this.formFather.compressFileId");
if (this.formFather.compressFileId == null) {
this.$_Methods.Message.warn("附件还未上传");
}
this.$_Api.Basics.Get({
url: "",
params: {
fileId: "",
},
}).then(res => {
//base64 数据流
this.viewPdfs(res);
});
},
viewPdfs(content) {
const blob = this.base64ToBlobs(content);
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob);
} else {
const fileURL = URL.createObjectURL(blob);
// window.open(fileURL);
const link = document.createElement("a");
link.href = fileURL;
// 设置下载的文件名
link.download = "123";
// 触发点击
document.body.appendChild(link);
link.click();
// 清理
document.body.removeChild(link);
URL.revokeObjectURL(fileURL);
}
},
base64ToBlobs(code) {
code = code.replace(/[\n\r]/g, "");
const raw = window.atob(code);
const rawLength = raw.length;
const uInt8Array = new Uint8Array(rawLength);
for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
// return new Blob([uInt8Array], { type: "application/pdf" }); 根据你上传的文件类型决定
return new Blob([uInt8Array], { type: `${"application" + "/" + this.formFather.compressType.replace(/\./, "")}` });
},
前端浏览器下载文件
最新推荐文章于 2024-10-15 15:24:52 发布