微信公众号用vue开发,用到下载功能,在公众号里面点击下载时提示“可在浏览器打开次网页来下载文件”,但在google浏览器点击下载可以自动下载。以下是所用代码,请问下有什么问题或者提供下其他方法,谢谢。
export function downloadPicture(imgSrc, name) {
const image = new Image()
// 解决跨域 Canvas 污染问题
image.setAttribute('crossOrigin', 'Anonymous')
image.src = imgSrc
image.onload = () => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const context = canvas.getContext('2d')
context.drawImage(image, 0, 0, image.width, image.height)
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob)
const eleLink = document.createElement('a')
eleLink.download = name || 'photo'
eleLink.href = url
eleLink.click()
eleLink.remove()
URL.revokeObjectURL(url)
})
}
}
真机预览图片没问题的,但是调用下载图片真机提示{'errMsg':'downloadImage:fail'}。红框log是微信开发者工具打印出来的
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#20