From d0207e5a082d0ba289dbbbcafa50283783029a7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=95=E9=98=B3=E5=BE=AE=E7=AC=911?= <12457268+sunset-smile-1@user.noreply.gitee.com> Date: Mon, 16 Dec 2024 13:41:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/download/index.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/api/download/index.ts diff --git a/src/api/download/index.ts b/src/api/download/index.ts new file mode 100644 index 0000000..d9ce1e4 --- /dev/null +++ b/src/api/download/index.ts @@ -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 }; \ No newline at end of file