实现一个下载图片的功能需求
效果图:
1.点击下载按钮 触发
<div class="down">
<el-button type="primary" @click.native="hendleDownload">
<i class="fas fa-download"> 下载</i>
</el-button>
</div>
2. 前端处理文件流
hendleDownload() {
const downloadList = this.$refs.multipleTable.selection;
if (downloadList.length < 1) {
this.$message({
type: "warning",
message: "请选中至少1张图片!",
});
return;
}
downloadOSSImage(downloadList).then((res) => {
//创建一个Blob对象接收后端传来的文件流,.zip的类型
const blob = new Blob([res.data], { type: "application/zip" });
if ("download" in document.createElement("a")) {
const link = document.createElement("a"); //创建a标签
link.style.display = "none";
// 创建下载的链接
link.href = window.URL.createObjectURL(blob);
link.download = "**Image.zip"
link.setAttribute(
"download",
res.headers["content-disposition"].slice(21, 29)
);
document.body.appendChild(link);
link.click(); //执行下载
URL.revokeObjectURL(link.href); //释放url
document.body.removeChild(link); //释放标签
}
})
},
以上就是下载图片的操作,大家可以参考参考,有什么问题可以留言哈~可以多多关注留言一下哈~