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

javascript - Turning an axios image stream into a string after Base64 Encoding it? - Stack Overflow

programmeradmin5浏览0评论

So far I have this:

const Fs = require('fs')  
const Path = require('path')  
const Axios = require('axios')
var {Base64Encode} = require('base64-stream');

const url = ''

const response = await Axios({
      url,
      method: 'GET',
      responseType: 'stream'
    })

response.data.pipe(new Base64Encode())

This base 64 encodes the image. How can we turn that into a string. I tried something like this:

  function streamToString (stream) {
    const chunks = []
    return new Promise((resolve, reject) => {
      stream.on('data', chunk => chunks.push(chunk))
      stream.on('error', reject)
      stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
    })
  }

const result = await streamToString(response.data.pipe(new Base64Encode()))

But it errors. Thoughts?

So far I have this:

const Fs = require('fs')  
const Path = require('path')  
const Axios = require('axios')
var {Base64Encode} = require('base64-stream');

const url = 'https://unsplash./photos/AaEQmoufHLk/download?force=true'

const response = await Axios({
      url,
      method: 'GET',
      responseType: 'stream'
    })

response.data.pipe(new Base64Encode())

This base 64 encodes the image. How can we turn that into a string. I tried something like this:

  function streamToString (stream) {
    const chunks = []
    return new Promise((resolve, reject) => {
      stream.on('data', chunk => chunks.push(chunk))
      stream.on('error', reject)
      stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
    })
  }

const result = await streamToString(response.data.pipe(new Base64Encode()))

But it errors. Thoughts?

Share asked Mar 27, 2020 at 21:42 OleOle 47.5k70 gold badges238 silver badges445 bronze badges 1
  • 1 Replace Buffer.concat(chunks).toString('utf8') with chunks.join("") – blex Commented Mar 27, 2020 at 22:01
Add a ment  | 

1 Answer 1

Reset to default 7

In your example you used the Base64encode module which already pumps out strings - so that made the above example fail (unless that was because of the top level await). You didn't actually need to convert the chunks to strings, since they already were strings, so a simple concatenation would do.

In fact the whole thing may be 10 times simpler with just using node.js streams:

const Axios = require('axios')
const {PassThrough} = require("stream");

const url = 'https://unsplash./photos/AaEQmoufHLk/download?force=true';

(async function() {
    const response = await Axios({
        url,
        method: 'GET',
        responseType: 'stream'
    });

    // we just pipe the data (the input carries it's own encoding)
    // to a PassThrough node stream that outputs `base64`.
    const chunks = response.data
        .pipe(new PassThrough({encoding:'base64'}));

    // then we use an async generator to read the chunks
    let str = '';
    for await (let chunk of chunks) {
        str += chunk;
    }

    // et voila! :)
    console.log(str);
})();
发布评论

评论列表(0)

  1. 暂无评论