1

实现一个小功能,远程url转为file对象
方案一:

function getFileFromUrl(url: string, fileName: string) {
  return new Promise(async (resolve, reject) => {
    const response = await fetch(url);
    const blob = await response.blob()
    let file = new File([blob!], fileName, { type: blob.type });
    resolve(file)
  })
}

方案二:

function getFileFromUrl(url: string, fileName: string) {
  return new Promise((resolve, reject) => {
    var blob = null;
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.responseType = "blob";
    // 加载时处理
    xhr.onload = () => {
      // 获取返回结果
      blob = xhr.response;
      let file = new File([blob!], fileName, { type: blob!.type });
      // 返回结果
      resolve(file);
    };
    xhr.onerror = (e) => {
      reject(e)
    };
    // 发送
    xhr.send();
  });
}

使用:

getFileFromUrl(url, name).then(res => {
})

参考文章


明未为洺
68 声望2 粉丝