export const downloadUrl = (href: string, fileName?: string) => {
const eleLink = document.createElement('a')
eleLink.download = fileName ?? ''
eleLink.href = href
eleLink.click()
eleLink.remove()
}
export const downloadByBlob = (src: string, name?: string) => {
const image = new Image()
image.setAttribute('crossOrigin', 'anonymous')
image.onload = () => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d')
if (ctx) {
ctx.drawImage(image, 0, 0, image.width, image.height)
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob as Blob)
downloadUrl(url, name)
// 用完释放URL对象
URL.revokeObjectURL(url)
})
}
}
image.src = src + (src.indexOf('?') > -1 ? '&time=' : '?time=') + new Date().valueOf()
}
01-11
841
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-21
1082
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-06
1929
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-06
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交