首页 > 其他分享 >Uncaught DONException: Failed to execute ‘atob‘ on “window ‘: The string to be decoded is not carrec

Uncaught DONException: Failed to execute ‘atob‘ on “window ‘: The string to be decoded is not carrec

时间:2022-09-22 13:44:32浏览次数:65  
标签:decoded execute string image base64 canvas let File new

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABGU....

报错是因为没有去除 data:image/png;base64,应只要后面的文件流

1. base64 : 可以直接当作链接地址使用

1 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABGU....

2. File 对象:

复制代码
File{
    lastModified: 1649490762164
    lastModifiedDate: Sat Apr 09 2022 15:52:42 GMT+0800 (GMT+08:00) {}
    name: "1649490762160-251.png"
    size: 25748
    type: "image/png"
    webkitRelativePath: ""
    [[Prototype]]: File
}
复制代码

3. Blob 对象

1 2 3 4 5 Blob{     size: 25748     type: "image/png"     [[Prototype]]: Blob } 

除了base64 可以直接当作链接使用,File 对象和Bolb 对象也可以通过转换 成base64 直接使用,或者使用 

window.URL.createObjectURL(File 对象或者Bolb 对象) 直接生成链接 : 类似:blob:null/22ec44b8-5864-4960-8ea8-a71a7e40bb63 所以:能直接当作链接是使用在这三者关系中有  
1 2 1. data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABGU.... 2. blob:null/22ec44b8-5864-4960-8ea8-a71a7e40bb63

  

  一:base64 转 Blob 对象  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 //base64 转 Blob对象 /* base64: data:image/png;base64,iVB.... /* Blob:{...} */ function base64ToBlob(data) {     var arr = data.split(','),         mime = arr[0].match(/:(.*?);/)[1],         bstr = atob(arr[1]),         n = bstr.length,         u8arr = new Uint8Array(n);       while (n--) {         u8arr[n] = bstr.charCodeAt(n);     }     return new Blob([u8arr], { type: mime }); }

  

 

二: base64 转 File 对象

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /** base64 转 File 对象   /* @param data base64   /* @param filename 自定义文件名   /* @return File 对象   */   function base64ToFile(data, filename) {       var arr = data.split(','), mime = arr[0].match(/:(.*?);/)[1],             bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);              let suffixArr = mime.split("/")       if (suffixArr.length && !filename) {           let suffix = suffixArr[suffixArr.length - 1]           filename = new Date().getTime() + "-" + Math.floor((Math.random() * 10000)) + "." + suffix       }         while (n--) {           u8arr[n] = bstr.charCodeAt(n);       }       return new File([u8arr], filename, { type: mime });   }

  

三: File 对象,Blob 对象 转base64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 //blob ,File 对象 转 base64 function blobFileTobase64(blobFile) {     let reader = new FileReader()     reader.readAsDataURL(blobFile);     return new Promise((resolve, reject) => {         reader.onload = function () {               resolve(this.result)         }         reader.onerror = (err) => {             reject(err);         };     }) }   

  

四: blob链接转 base64 :利用canvas.toDataURL的API转化成base64  
1 2 blob:null/22ec44b8-5864-4960-8ea8-a71a7e40bb63 转为 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABGU....

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 function urlToBase64(url, type = "image/png") {     return new Promise((resolve, reject) => {         let img = new Image()         img.src = url         img.onload = function () {             let canvas = document.createElement("canvas")             canvas.width = this.naturalWidth             canvas.height = this.naturalHeight             let ctx = canvas.getContext("2d")             ctx.drawImage(img, 0, 0)             let result = canvas.toDataURL(type || 'image/png')             resolve(result)         }         img.onerror = function (err) {             reject(err)         }     })   }   

  

  

      五: 压缩图片:接收一个File/Blob 对象,返回的是一个base64

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 /*           blobFile:file/blob 对象           option:{type:"image/jpeg",width:'',height:"",quality:0.92,scale:1}       */       function compressImg(blobFile, option = {}) {           return new Promise((resolve, reject) => {               let reader = new FileReader()               reader.onload = function () {                   let img = new Image()                   img.src = this.result                   img.onload = function () {                       console.log("原图片大小:"this.src.length);//这是base64 ,所以可以指定图片大小                       let imgSize = this.src.length;                       let targetSize = 100 * 1024;                       if (imgSize <= targetSize) {                           return resolve(this.src)                       }                       let canvas = document.createElement("canvas")                       let ctx = canvas.getContext("2d")                       //原图尺寸                       let imgW = this.naturalWidth                       let imgH = this.naturalHeight                         //新图比例大小                       let scale = option.scale || 1;                         let ratio = imgW / imgH; //宽高比例                       let targetW = option.width || imgW * scale                       let targetH = option.height || imgH * scale                       canvas.width = targetW                       canvas.height = targetH                       ctx.drawImage(img, 0, 0, targetW, targetH)                       let fileType = blobFile.type || option.type || 'image/jpeg'                       let quality = option.quality || 0.5                       let result = canvas.toDataURL(fileType, quality)                       console.log("压缩后:", result.length)                       resolve(result)                   }                 }               reader.onerror = function () {                   reject(new Error(`${blobFile} must is Blob or File 对象`))               }               reader.readAsDataURL(blobFile)             })         }

标签:decoded,execute,string,image,base64,canvas,let,File,new
From: https://www.cnblogs.com/hellofangfang/p/16718958.html

相关文章

  • jdk9为何要将String的底层实现由char[]改成了byte[]
    作者:basic13链接:https://www.zhihu.com/question/447224628/answer/1759986003来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。这个特性是......
  • 记录nodejs中querystring‘已弃用’三种处理方法
    一.升级node版本,修改引入方式//升级到18.x版本后修改引入方式constquerystring=require('node:querystring')二.官方推荐URLSearchParams替代因为不想升级就按照......
  • string转wstring
    转载来自:https://www.cnblogs.com/coolbear/archive/2013/05/24/3096683.htmlusingnamespacestd;1、字符串中不含有汉字|--string-->wstringstrings("abc");ws......
  • JDK8中String的intern()方法详细解读【内存图解+多种例子+1.1w字长文】
    写在前面,欢迎大家关注小编的微信公众号!!谢谢大家!!一、前言String字符串在我们日常开发中最常用的,当然还有他的两个兄弟StringBuilder和StringBuilder。他三个的区别也是面......
  • Codeforces Round #820 (Div. 3) G. Cut Substrings
    DPProblem-G-Codeforces题意给一个长度为\(n(1<=n<=500)\)的主串s,一个长度为\(m(1<=m<=500)\)的模式串t,每次可以将当前的s中与t相同的子串变成一串"."(如......
  • STL string的查找,比较,截取子串
    compare函数在>时返回1,<时返回-1,==时返回0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。intcompare(conststring&s)const;//与字符串s......
  • list 转 string 以逗号隔开
    System.out.println(String.join(",",deptNameList));System.out.println(CollectionUtil.join(deptNameList,","));//ArrayUtil得要这种格式String[]System.out.printl......
  • String、StringBuffer和StringBuilder区别
    String、StringBuffer和StringBuilder区别1、StringBuffer和StringBuilder非常的类似,均表示字符可变序列,方法也一样。2、String表示不可变的字符序列,效率低,复用性高。3......
  • [杂项]World.execute(me)
    \[\mathit{Switch~on~the~power~line}\]\[\mathit{Remember~to~put~on}\]\[\mathit{PROTECTION}\]\[\mathit{Lay~down~your~pieces}\]\[\mathit{And~let~^\primes~beg......
  • hive substring_index 函数
    substring_index详解substring_index(str,delim,count)str:要处理的字符串delim:分隔符count:计数例子:str=www.wikibt.comsubstring_index(str,'.',1)......