improve image download with blob conversion for cross-origin support
This commit is contained in:
parent
435c4740b9
commit
67a117f65f
@ -487,17 +487,29 @@ const handleDownload = async () => {
|
|||||||
if (!resultImage.value) return
|
if (!resultImage.value) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 获取图片数据并转换为 Blob,确保跨域图片也能下载
|
||||||
|
const response = await fetch(resultImage.value)
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const blob = await response.blob()
|
||||||
|
const blobUrl = URL.createObjectURL(blob)
|
||||||
|
|
||||||
|
// 创建下载链接
|
||||||
const link = document.createElement('a')
|
const link = document.createElement('a')
|
||||||
link.href = resultImage.value
|
link.href = blobUrl
|
||||||
link.download = `removed-background-${Date.now()}.png`
|
link.download = `removed-background-${Date.now()}.png`
|
||||||
link.target = '_blank'
|
|
||||||
link.style.display = 'none'
|
link.style.display = 'none'
|
||||||
|
|
||||||
document.body.appendChild(link)
|
document.body.appendChild(link)
|
||||||
link.click()
|
link.click()
|
||||||
|
|
||||||
setTimeout(() => {
|
// 清理:移除 DOM 元素并释放 blob URL
|
||||||
|
requestAnimationFrame(() => {
|
||||||
document.body.removeChild(link)
|
document.body.removeChild(link)
|
||||||
}, 100)
|
URL.revokeObjectURL(blobUrl)
|
||||||
|
})
|
||||||
|
|
||||||
message.success(t('Download started'))
|
message.success(t('Download started'))
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user