使用 XMLHttpRequest 对象获取图片url的Blob值或转成file

使用 XMLHttpRequest 对象获取图片url的Blob值

getImageBlob (url, cb) {
  let xhr = new XMLHttpRequest();
  xhr.open('get', url, true);
  xhr.responseType = 'blob';
  xhr.onload = function () {
    if (this.status === 200) {
      if (cb) {
        cb(this.response);
      }
    }
  };
  xhr.send();
}
Blob值

转成file对象

this.getImageBlob('图片地址', function (blob) {
  const file = new window.File([blob], `文件名.${blob.type.split('/')[1]}`, { type: blob.type });
  console.log('blob', blob);
  console.log('file', file);
}
使用 XMLHttpRequest 对象获取图片url的Blob值或转成file

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注