简体   繁体   中英

How to access data from ReadableStream response?

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM