UNPKG

@planjs/utils

Version:

🔧 Common tools collection

39 lines (33 loc) 945 B
/** * 通过myFetch获取远程资源blob * @param url * @param options */ function fetchBlob(url, options) { return new Promise(function (resolve, reject) { var xhr = new XMLHttpRequest(); xhr.open('get', url, true); xhr.responseType = 'blob'; xhr.withCredentials = (options === null || options === void 0 ? void 0 : options.credentials) === 'include'; if (options !== null && options !== void 0 && options.headers) { for (var i in options === null || options === void 0 ? void 0 : options.headers) { xhr.setRequestHeader(i, options.headers[i]); } } xhr.onload = function () { if (this.status === 200) { resolve(this.response); } else { reject(this.response); } }; xhr.onerror = function (error) { reject(error); }; xhr.ontimeout = function (error) { reject(error); }; xhr.send(); }); } export default fetchBlob;