Skip to content

Commit afcb7e7

Browse files
committed
【解决todo】图片下载抽离到 download
1 parent 557dc7d commit afcb7e7

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/utils/download.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,22 @@ const download = {
3636
}
3737

3838
export default download
39+
40+
/** 图片下载(通过浏览器图片下载) */
41+
export const downloadImage = async (imageUrl) => {
42+
const image = new Image()
43+
image.setAttribute('crossOrigin', 'anonymous')
44+
image.src = imageUrl
45+
image.onload = () => {
46+
const canvas = document.createElement('canvas')
47+
canvas.width = image.width
48+
canvas.height = image.height
49+
const ctx = canvas.getContext('2d') as CanvasDrawImage
50+
ctx.drawImage(image, 0, 0, image.width, image.height)
51+
const url = canvas.toDataURL('image/png')
52+
const a = document.createElement('a')
53+
a.href = url
54+
a.download = 'image.png'
55+
a.click()
56+
}
57+
}

src/views/ai/image/ImageTask.vue

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import ImageDetailDrawer from './ImageDetailDrawer.vue'
3333
import ImageTaskCard from './ImageTaskCard.vue'
3434
import { ElLoading, LoadingOptionsResolved } from 'element-plus'
3535
import { AiImageStatusEnum } from '@/views/ai/utils/constants'
36+
import { downloadImage } from '@/utils/download'
3637
3738
const message = useMessage() // 消息弹窗
3839
@@ -150,26 +151,6 @@ const handleImageMjBtnClick = async (button: ImageMjButtonsVO, imageDetail: Imag
150151
await getImageList()
151152
}
152153
153-
/** 下载 - image */
154-
// TODO @fan:貌似可以考虑抽到 download 里面,作为一个方法
155-
const downloadImage = async (imageUrl) => {
156-
const image = new Image()
157-
image.setAttribute('crossOrigin', 'anonymous')
158-
image.src = imageUrl
159-
image.onload = () => {
160-
const canvas = document.createElement('canvas')
161-
canvas.width = image.width
162-
canvas.height = image.height
163-
const ctx = canvas.getContext('2d') as CanvasDrawImage
164-
ctx.drawImage(image, 0, 0, image.width, image.height)
165-
const url = canvas.toDataURL('image/png')
166-
const a = document.createElement('a')
167-
a.href = url
168-
a.download = 'image.png'
169-
a.click()
170-
}
171-
}
172-
173154
// page change
174155
const handlePageChange = async (page) => {
175156
pageNo.value = page

0 commit comments

Comments
 (0)