// url img地址,图片地址如果是网络图片,网络地址需要处理跨域 // fn 函数,返回一个blob对象 imageToBlob (url, fn) { if (!url || !fn) return false; var xhr = new XMLHttpRequest(); xhr.open('get', url, true); xhr.responseType = 'blob'; xhr.onload = function () { // 注意这里的this.response 是一个blob对象 就是文件对象 fn(this.status == 200 ? this.response : false); } xhr.send(); return true; }
PS:Blob对象转file对象,请看随笔:https://www.cnblogs.com/chensv/p/15433991.html