PixelAI-admin/src/api/download/index.ts

25 lines
717 B
TypeScript
Raw Normal View History

2024-12-16 13:41:43 +08:00
import axios from "axios";
2024-12-18 14:30:37 +08:00
import { ElMessage } from "element-plus";
2024-12-16 13:41:43 +08:00
const downloadPhoto = (imgSrc:any) => {
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 = '下载图片.jpg';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
2024-12-18 14:30:37 +08:00
ElMessage({message: '图片下载成功!',type: 'success',});
2024-12-16 13:41:43 +08:00
}).catch(error => {
2024-12-18 14:30:37 +08:00
ElMessage({message: '图片下载失败!',type: 'error',});
2024-12-16 13:41:43 +08:00
});
};
export { downloadPhoto };