2017-07-27 65 views
0

我构建了一个REST API,它返回一个图像,并且我通过PostMan代理成功获取其响应。 PostMan response 在我的阵营代码,我有这样的:ReactJS:无法从REST API响应在UI中呈现图像

return fetch(url, sInit) 
    .then((response) => { 
    let blb = new Blob([response.body], { type: 'image/jpeg' }); 
    let fileUrl = (window.URL || window.webkitURL).createObjectURL(blb); 
    window.open(fileUrl); 
    }) 

产生的不具有图像团块网址。我错过了什么?

回答

1

您可能需要使用内置blob()方法,通过fetch

fetch(
 
    'https://images.unsplash.com/35/SRkdurVYQFSFkMyHVwSu_spinnenweb-9536.jpg?dpr=2&auto=compress,format&fit=crop&w=376&h=251&q=80&cs=tinysrgb&crop=' 
 
) 
 
    .then(res => res.blob()) 
 
    .then(blob => { 
 
    let fileUrl = (window.URL || window.webkitURL).createObjectURL(blob); 
 

 
    const img = document.createElement('img'); 
 
    img.src = fileUrl; 
 
    document.querySelector('body').appendChild(img); 
 
    });

+0

你的答案是有很大帮助的,但是这并不在IE 8至11 –

+0

没有工作你包含['fetch' polyfill](https://github.com/github/fetch)?这些浏览器的[fetch] [不支持](http://caniuse.com/#feat=fetch)。 –

+0

请检查文档,在IE10及更高版本中支持'fetch',但任何版本的Internet Explorer都不支持'blob()'。 –