无法通过简单的提取创建Blob

时间:2019-10-10 16:54:31

标签: javascript

我需要使用从节点服务器获取的内容来设置src标签的<img>属性。

function fetchPicture(path) {
  fetch(path)
    .then(function(response) {
      return response.blob();
    })
    .then(function(myBlob) {
      return window.URL.CreateObjectURL(myBlob);
    });
}

但是,在我的.then(response)中处理诺言时,我得到了pathToMyHTMLFile / undefined的文件未找到异常。

我的路径属于http://localhost:8080/Images/fig3.png类型,响应的状态为200

任何提示将不胜感激!

1 个答案:

答案 0 :(得分:0)

您的函数未返回任何内容。您正在从回调函数返回URL,但这不会自动弹出。取而代之的是,该函数应返回整个提取承诺,然后在调用时异步处理它,例如fetchPicture('http://url.path.here').then(url => image.src = url)或类似的东西。