vue使用html2canvas并进行下载
一、引包npm install --savehtml2canvas二、导入import html2canvas from 'html2canvas'三、web端下载html2canvas(document.querySelector('#这是需要导出的dom的id')).then(canvas => {let saveUrl = canva...
·
一、引包
npm install --save html2canvas
二、导入
import html2canvas from 'html2canvas'
三、web端下载
html2canvas(document.querySelector('#这是需要导出的dom的id')).then(canvas => {
let saveUrl = canvas.toDataURL('image/png')
let a = document.createElement('a')
document.body.appendChild(a)
a.href = saveUrl
a.download = '这是图片标题'
a.click()
})
四、移动端下载
// 转换方法
base64ToBlob (code) {
let parts = code.split(';base64,')
let contentType = parts[0].split(':')[1]
let raw = window.atob(parts[1])
let rawLength = raw.length
let uInt8Array = new Uint8Array(rawLength)
for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i)
}
return new Blob([uInt8Array], {type: contentType})
},
// 具体调用的方法
async save() {
html2canvas(document.querySelector('#需要导出元素的id')).then(canvas => {
let saveUrl = canvas.toDataURL('image/png')
let aLink = document.createElement('a')
let blob = this.base64ToBlob(saveUrl)
let evt = document.createEvent('HTMLEvents')
evt.initEvent('click', true, true)
aLink.download = '标题.jpg'
aLink.href = URL.createObjectURL(blob)
aLink.click()
})
},
更多推荐
已为社区贡献3条内容
所有评论(0)