一些有用的方法记录
作者:互联网
记一下,免得用的时候,找到的都是坑
//图片url转base64
imgUrlToBase64 = imgObj => { return new Promise((resolve,reject) => { const img = document.createElement('img') img.setAttribute('crossOrigin', 'anonymous'); img.src = imgObj.url img.onload = () => { const canvas = document.createElement('canvas') canvas.height = img.height canvas.width = img.width const ctx = canvas.getContext('2d') ctx.drawImage(img,0,0,img.width,img.height) const base64 = canvas.toDataURL("image/png") resolve({base64,name:imgObj.name}) } }) }export const downloadFile = (data, fileName) => { const blob = new Blob([data]); if ('download' in document.createElement('a')) { // 非IE下载 const elink = document.createElement('a'); elink.download = fileName; elink.style.display = 'none'; elink.href = URL.createObjectURL(blob); document.body.appendChild(elink); elink.click(); URL.revokeObjectURL(elink.href); // 释放URL 对象 document.body.removeChild(elink); } else { // IE10+下载 navigator.msSaveBlob(blob, fileName); } }; //常见深拷贝 export const deepClone = (obj, map = new Map()) => { if (!(obj instanceof Object)) return obj // 原始数据类型 if (map.has(obj)) return map.get(obj) // 重复引用 let result = Array.isArray(obj) ? [] : {} map.set(obj, result) // 将对象存入map // 遍历所有属性进行递归拷贝 ;[...Object.keys(obj), ...Object.getOwnPropertySymbols(obj)].forEach(key => { result[key] = deepClone(obj[key], map) }) return result }
//下载文件
标签:canvas,const,img,记录,elink,有用,obj,document,方法 来源: https://www.cnblogs.com/chchchc/p/15904494.html