// 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