图片下载

This commit is contained in:
夕阳微笑1 2024-12-16 13:41:43 +08:00
parent 16bc099987
commit d0207e5a08

23
src/api/download/index.ts Normal file
View File

@ -0,0 +1,23 @@
import axios from "axios";
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);
}).catch(error => {
console.error('Download error:', error);
});
};
export { downloadPhoto };