import axios from "axios"; import { ElMessage } from "element-plus"; const downloadPhoto = (imgSrc:any) => { // 这个直接虚拟dom点击,不然会有跨域问题 if(imgSrc.includes('guojunjie.oss-cn-hangzhou.aliyuncs.com')){ const a = document.createElement('a'); a.href = imgSrc; a.download = '下载图片.'+ imgSrc.substr(imgSrc.lastIndexOf('.')+1) || 'jpg'; document.body.appendChild(a); a.click(); document.body.removeChild(a); ElMessage({message: '图片下载成功!',type: 'success',}); return; } axios({ url: imgSrc, method: 'get', headers: { 'Content-Type': 'application/json' }, responseType: 'blob', }).then(res => { const url = window.URL.createObjectURL(res.data); const a = document.createElement('a'); a.href = url; a.download = '下载图片.'+ imgSrc.substr(imgSrc.lastIndexOf('.')+1) || 'jpg'; document.body.appendChild(a); a.click(); document.body.removeChild(a); window.URL.revokeObjectURL(url); ElMessage({message: '图片下载成功!',type: 'success',}); }).catch(error => { ElMessage({message: '图片下载失败!',type: 'error',}); }); }; export { downloadPhoto };