2 个回答

获取文件对象的blob路径:

getObjectURL(file) {
  let url = null
  if (window.createObjectURL !== undefined) {
    // basic
    url = window.createObjectURL(file)
  } else if (window.webkitURL !== undefined) {
    // webkit or chrome
    url = window.webkitURL.createObjectURL(file)
  } else if (window.URL !== undefined) {
    // mozilla(firefox)
    url = window.URL.createObjectURL(file)
  }
  return url // blob url
}

用xhr。

仅限同源图片

  const xhr = new XMLHttpRequest();
  xhr.withCredentials = true;

  xhr.addEventListener('readystatechange', function() {
    if (this.readyState === 4) {
      console.log(this.response); // blob
      const blob_url = URL.createObjectURL(this.response);
      const img = new Image();
      img.src = blob_url;
      document.body.appendChild(img);
    }
  });

  xhr.open('get', './1.jpg');
  xhr.responseType = 'blob';

  xhr.send();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题