JS 图片转blob 转base64

 1 //转换为blob有跨域限制
 2 var loadImageToBlob = function (url, callback) {
 3 
 4   if (!url || !callback) return false;
 5   var xhr = new XMLHttpRequest();
 6   xhr.open('get', url, true);
 7   xhr.responseType = 'blob';
 8   xhr.onload = function () {
 9     // 注意这里的this.response 是一个blob对象 就是文件对象
10     callback(this.status == 200 ? this.response : false);
11   }
12   xhr.send();
13   return true;
14 
15 }
16  $("#app-wraper img").each(function () {
17           loadImageToBlob($(this).attr("src"), function (blobFile) {
18             if (!blobFile) return false;
19             var reader = new FileReader();
20             //将文件以Data URL形式读入页面
21             reader.readAsDataURL(blobFile);
22             reader.οnlοad = function (e) {
23               //显示文件
24               var h = '<img src="' + this.result + '" alt="" />';
25               $("#result").append(h);
26             }
27           });
28         });

 

posted @ 2019-08-22 16:57  MollyHan  阅读(3149)  评论(0编辑  收藏  举报