0

I have built a REST API that returns an image and I am successfully getting its response through PostMan agent. PostMan response In my React Code, I have this:

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

The blob url generated does not have the image. What am I missing?

1 Answer 1

1

You probably want to use the built-in blob() method by 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);
  });

Sign up to request clarification or add additional context in comments.

3 Comments

Your answer is of great help, but this does not work in IE 8 through 11.
Did you include a fetch polyfill? fetch is not supported by these browsers.
Please check the documentation, fetch is supported in IE10 and above, but blob() is not supported in any version of Internet Explorer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.