最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access data from ReadableStream response? - Stack Overflow

programmeradmin2浏览0评论

I made an API call to an endpoint and it returns this:

const test = () => {
    fetch(endpoint)
      .then((response) => {
        console.log(response.body)
      })
      .catch((err) => {
        console.log(err);
      });
  };

How can I obtain the ReadableStream in a Base64 format? This is returning a png file.

I made an API call to an endpoint and it returns this:

const test = () => {
    fetch(endpoint)
      .then((response) => {
        console.log(response.body)
      })
      .catch((err) => {
        console.log(err);
      });
  };

How can I obtain the ReadableStream in a Base64 format? This is returning a png file.

Share Improve this question edited Sep 6, 2020 at 17:56 Daniel A. White 191k49 gold badges379 silver badges466 bronze badges asked Sep 6, 2020 at 17:53 Freddy.Freddy. 1,7453 gold badges23 silver badges36 bronze badges 1
  • 1 have you tried reading the fetch documentation? – Daniel A. White Commented Sep 6, 2020 at 17:57
Add a ment  | 

1 Answer 1

Reset to default 6

Using this answer, you can get the blob and convert it to base64.

const test = () => {
    fetch(endpoint)
      .then((response) => {
        return response.blob();
      })
      .then((blob)=>{
          var reader = new FileReader();
          reader.readAsDataURL(blob); 
          reader.onloadend = function() {
              var base64data = reader.result;                
              console.log(base64data);
          }
      })
      .catch((err) => {
        console.log(err);
      });
  };

MDN on .blob()

发布评论

评论列表(0)

  1. 暂无评论