- // 转换为 blob 有跨域限制
- var loadImageToBlob = function (url, callback) {
- if (!url || !callback) return false;
- var xhr = new XMLHttpRequest();
- xhr.open('get', url, true);
- xhr.responseType = 'blob';
- xhr.onload = function () {
- // 注意这里的 this.response 是一个 blob 对象 就是文件对象
- callback(this.status == 200 ? this.response : false);
- }
- xhr.send();
- return true;
- }
- $("#app-wraper img").each(function () {
- loadImageToBlob($(this).attr("src"), function (blobFile) {
- if (!blobFile) return false;
- var reader = new FileReader();
- // 将文件以 Data URL 形式读入页面
- reader.readAsDataURL(blobFile);
reader.οnlοad = function (e) {
- // 显示文件
- var h = '<img src="' + this.result + '"/>';
- $("#result").append(h);
- }
- });
- });
来源: http://www.bubuko.com/infodetail-3165797.html