Download an image using Axios and convert it to base64

后端 未结 3 857
忘了有多久
忘了有多久 2020-11-29 02:11

I need to download a .jpg image from a remote server and convert it into a base64 format. I\'m using axios as my HTTP client. I\'ve tried issuing a git request to the server

3条回答
  •  再見小時候
    2020-11-29 02:39

    This is what works for me (with Buffer.from) -

    axios
      .get(externalUrl, {
        responseType: 'arraybuffer'
      })
      .then(response => {
        const buffer = Buffer.from(response.data, 'base64');
      })
      .catch(ex => {
        console.error(ex);
      });
    

提交回复
热议问题