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

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

作者: 冰落寞成 | 来源:发表于2024-03-04 17:40 被阅读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
    });

相关文章

网友评论

      本文标题:js 将网络图片转换成文档流

      本文链接:https://www.haomeiwen.com/subject/mdfqzdtx.html