2017-09-05 38 views
0

我可以看到API响应中返回的图像,但无法将其设置为IE 10/11中的页面中img元素的src。 在Chrome中,此ReactJS代码工作正常:IE 10/11:来自API响应的图像预览

return fetch(url, sInit) 
     .then(response => response.blob()) 
     .then((blobResponse) => { 
     let fileUrl = (window.URL || window.webkitURL).createObjectURL(blobResponse); 
     // setting fileUrl as src for <img> 
     }); 

这fileUrl然后被设定为一个img元素的src属性。

但我需要为IE 10/11的解决方案,以便可以在指定的位置预览图像。

说明:window.navigator.msSaveOrOpenBlob的解决方法不是我所需要的。

+1

您使用的填充工具为'取()'在IE10/11? (因为'fetch()'[不支持IE10/11](https://caniuse.com/#search=fetch))。 – K3N

+0

是的,这是正确的。 –

回答

0

我能在这下面的方法来实现:

return fetch(url, sInit) 
    .then(response => response.blob()) 
    .then((blobResponse) => { 
    if (window.navigator.msSaveOrOpenBlob) { // Checking if IE 
     let reader = new FileReader(); 
     let dataURL = ''; 
     reader.onload = function readerFile(e) { 
     dataURL = reader.result; 
     // setting dataURL as src for <img> 
     }; 
     reader.readAsDataURL(blobResponse); 
    } else { 
     let fileUrl = (window.URL || window.webkitURL).createObjectURL(blobResponse); 
     // setting fileUrl as src for <img> 
    } 
    });