2017-07-27 3 views
0

Ich habe eine REST API erstellt, die ein Bild zurückgibt und ich bekomme erfolgreich seine Antwort über PostMan-Agent. PostMan response In meinem React-Code, ich habe dies:ReactJS: Kann Bild in UI nicht von REST API response

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); 
    }) 

Die URL blob das Bild nicht erzeugt haben. Was vermisse ich?

Antwort

1

Sie wollen wahrscheinlich die eingebaute in blob() Methode verwenden, von 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

Ihre Antwort eine große Hilfe ist, aber das funktioniert nicht in IE 8 bis 11 –

+0

Did Sie schließen ein ['fetch' Polyfill] (https://github.com/github/fetch) ein? 'fetch' wird von diesen Browsern nicht unterstützt (http://caniuse.com/#feat=fetch). –

+0

Bitte überprüfen Sie die Dokumentation, 'fetch' wird in IE10 und höher unterstützt, aber' blob() 'wird in keiner Version von Internet Explorer unterstützt. –