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

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')
      })
    },

  

上一篇:两台电脑使用一套鼠键


下一篇:文件下载的写法