vue3实现图片的下载

写一个工具类,放到src/utils文件夹下新建文件previewDownFile.js

复制代码
import axios from "axios";
import { ElMessage} from "element-plus";
 
export function previewBtn(url,fileName) { // url-下载预览地址 fileName-文件名
    if (!fileName) { // 图片预览
        window.open(url);
  }else{  // 文件下载
    axios({
      url,
      method: "GET",
      responseType: "blob"
    })
      .then(res => {
        const elink = document.createElement("a");
        elink.download = fileName;
        elink.style.display = "none";
        elink.href = URL.createObjectURL(res.data);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL对象
        document.body.removeChild(elink);
        ElMessage.success("下载成功")
      })
      .catch(() => {
        ElMessage.error("下载失败!");
      });
    }
}

引入

import { previewDownFile } from "@/utils/previewDownFile";

调用

复制代码
<div class="downLoadImg svgBox">
    <el-icon size="30" @click="previewBtn(item,item.projectName+'_附件'+(index+1))">
        <Download />
    </el-icon>
</div>

vue3导入icon图标

import { Download } from '@element-plus/icons-vue';

相关推荐
一木 之林7 分钟前
LangChain 第6章 Agent 智能体核心:create_agent 统一入口与 ReAct 循环
前端·react.js·langchain
不可能片场26 分钟前
Electron 发布进程被回收 用脱离进程组托住
前端·electron
kaixin_learn_qt_ing32 分钟前
hexdump -C /path/to/binary | less 看十六进制内容
前端·css·less
java1234_小锋34 分钟前
【技术专题】HTML5 网页技术 - HTML5 链接与图片
前端·html·html5
sun_weitao36 分钟前
Chrome 扩展插件开发指南
前端·javascript·chrome
小马不起床1 小时前
零基础玩转 FastAPI_12:前端详情增强与角色权限分配
前端·状态模式·fastapi
labview_自动化1 小时前
cvat部署记录
javascript·labview
前端snow1 小时前
ai agent --- 切割文件 RecursiveCharacterTextSplitter
前端
松就是我902981 小时前
可编辑 PPT:调研、设计与实施方案
前端
前端snow1 小时前
ai agent --- Elasticsearch 混合检索
前端