js 将网络图片转换成文档流

2024-03-04  本文已影响0人  冰落寞成

转换函数

const changeBlob=(url)=> {
  return new Promise((resolve) => {
   
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "blob";
      xhr.onload = () => {
          if (xhr.status === 200) {
            
              resolve(xhr.response);
          }
      };
      xhr.send();
  });
}

调用函数

 changeBlob(url).then((res) => {
      var nameArr = url.split("/");
      const files = new File([res], nameArr[nameArr.length - 1], {
          type: res.type,
      });
      ruleForm.file = files
    });
上一篇 下一篇

猜你喜欢

热点阅读