vue文件上传下载(仅script方法)
//上传成功钩子
success_1(response, file, fileList) {
console.log(response.originalName);
let str = response.originalName;
let index = str.lastIndexOf(".");
str = str.substring(index + 1, str.length);
this.downType = str;
this.originalName_1 = response.originalName;
this.remoteFileName_1 = response.remoteFileName;
},
//下载
async down_1() {
const {
data: res
} = await this.$http.get(
"5003/sys/file/download?originalName=" +
this.originalName_1 +
"&" +
"remoteFileName=" +
this.remoteFileName_1,
{ responseType: "blob" }
);
let data = res;
if (!data) {
this.$message.error("下载失败,解析数据为空!");
return;
}
const datetime = Date.now();
// 创建一个新的url,此url指向新建的Blob对象
let url = window.URL.createObjectURL(new Blob([data]));
// 创建a标签,并隐藏改a标签
let link = document.createElement("a");
link.style.display = "none";
// a标签的href属性指定下载链接
link.href = url;
//setAttribute() 方法添加指定的属性,并为其赋指定的值。
let fileName = datetime + "." + this.downType;
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
this.$message.success("导出成功");
}