ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

点击按钮保存当前页面为图片----html2canvas

2021-11-25 18:01:36  阅读:20  来源: 互联网

标签:---- donwLink dataurl ele html2canvas let download 页面


npm install --save html2canvas

import html2canvas from 'html2canvas'

  

handleDownload (id) {
      let ele = document.getElementById(id)
      let ignoreElements = ele.querySelector('.chart-download')
      html2canvas(ele, {
        logging: false,
        ignoreElements: (elements) => { //  忽略的ele
          return elements === ignoreElements
        }
      }).then(function (canvas) {
        // canvas转为图片流
        let dataurl = canvas.toDataURL('image/png')
        console.log(dataurl)

        // 非ie下正常,主要原因是ie下a标签不支持download属性
        let donwLink = document.createElement('a')
        donwLink.href = dataurl
        donwLink.download = '统计分析' + new Date().getTime() // 图片名字
        donwLink.click()
        let event = new MouseEvent('click')
        donwLink.dispatchEvent(event)

        // 使用downloadjs兼容ie下下载,https://github.com/rndme/download
        // download(data, strFileName, strMimeType);
        // downloadjs(dataurl, `统计分析${new Date().getTime()}`, 'image/png')
      })
    },

  

标签:----,donwLink,dataurl,ele,html2canvas,let,download,页面
来源: https://www.cnblogs.com/cs122/p/15603904.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有