PixelAI-admin/src/api/download/index.ts
2024-12-27 15:03:48 +08:00

37 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 };