vue 截图转base64转文件File 异步获取

复制代码
import html2canvas from "html2canvas";
export   function  base64ImgtoFile(dataurl, filename = 'file') {
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let suffix = mime.split('/')[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], `${filename}.${suffix}`, {
      type: mime
    })
  }


  export   function toImages(imageTofile) {
    return new Promise((resolve, reject) => {
      html2canvas(imageTofile, {
        backgroundColor: null,
        useCORS: true 
      }).then((canvas) => {
        const url = canvas.toDataURL('image/png');
          resolve(base64ImgtoFile(url))
      }).catch((err) => {
        reject(err)
      })
    })
  }

// 调用

toImage() {

let that=this;

toImages(this.$refs.imageTofile).then((imgFileRaw) => {

that.imgFileRaw = base64ImgtoFile(this.htmlUrl);

}).catch((error) => {

that.imgFileRaw = null;

});

},

相关推荐
半点寒12W2 分钟前
微信小程序实现路由拦截的方法
前端
某公司摸鱼前端1 小时前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
要加油哦~1 小时前
vue | 插件 | 移动文件的插件 —— move-file-cli 插件 的安装与使用
前端·javascript·vue.js
小林学习编程1 小时前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot
柳鲲鹏1 小时前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
weixin-a153003083162 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
ai小鬼头3 小时前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
wen's3 小时前
React Native 0.79.4 中 [RCTView setColor:] 崩溃问题完整解决方案
javascript·react native·react.js
一只叫煤球的猫3 小时前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈
vvilkim3 小时前
Electron 自动更新机制详解:实现无缝应用升级
前端·javascript·electron