Node.js 如何从Buffer.from获得与fs.readFile相同的输出?

Node.js 如何从Buffer.from获得与fs.readFile相同的输出?,node.js,encoding,axios,buffer,fs,Node.js,Encoding,Axios,Buffer,Fs,我正在为我的slackbot开发一个功能,将二进制数据的post请求从一个图像发送到一个名为servicenow的票务系统。当时,我正努力从Nodejs获得正确的输出。我使用axios从url获取图像,然后将数据发送到Buffer.from并使用binary,这无法提供所需的正确输出。然而,使用fs.readFile似乎也会返回一个缓冲区对象,这就是我需要的输出 的文档说明readFile的编码设置为null。文档中表示默认编码为utf8 const fs = require("mz/fs");

我正在为我的slackbot开发一个功能,将二进制数据的post请求从一个图像发送到一个名为servicenow的票务系统。当时,我正努力从Nodejs获得正确的输出。我使用axios从url获取图像,然后将数据发送到Buffer.from并使用binary,这无法提供所需的正确输出。然而,使用fs.readFile似乎也会返回一个缓冲区对象,这就是我需要的输出

的文档说明readFile的编码设置为null。文档中表示默认编码为utf8

const fs = require("mz/fs");
const axios = require("axios");

const main = async () => {
  try {
    const attachImage = await axios.get(
      "https://d17fnq9dkz9hgj.cloudfront.net/uploads/2013/09/cat-black-superstitious-fcs-cat-myths-162286659.jpg"
    );

    const { data } = attachImage;

    // console.log(attachImage);

    const imgBuffer = Buffer.from(data, "binary");

    console.log(imgBuffer);

    const testImg = await fs.readFile("./cat1.jpg");

    console.log(testImg);


  } catch (err) {
    console.error(err);
  }
};

main();

第二个console.log输出(fs.readFile)是所需的输出。如何使用GET请求的这个精确输出进行blob的缓冲区输出

$node index.js

我相信您在运行axios.get()方法时遇到了编码问题。axios可以为您提供缓冲区,而不是从响应中创建缓冲区。尝试将您的请求更改为:

const fs = require("mz/fs");
const axios = require("axios");

const main = async () => {
  try {
    const attachImage = await axios.get(
      "https://d17fnq9dkz9hgj.cloudfront.net/uploads/2013/09/cat-black-superstitious-fcs-cat-myths-162286659.jpg",
      {
        responseType: 'arraybuffer',
        responseEncoding: 'binary'
      }
    );

    const { data } = attachImage;
    console.log(data);

    const testImg = await fs.readFile("./cat1.jpg", { encoding: null });
    console.log(testImg);


  } catch (err) {
    console.error(err);
  }
};

main();

当我在本地运行时,它们对我来说是相同的

$ node test.js
<Buffer ff d8 ff e1 0c 9f 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 0e 01 00 00 03 00 00 00 01 0f 30 00 00 01 01 00 03 00 00 00 01 0a 20 00 00 01 02 00 03 ... >
<Buffer ff d8 ff e1 0c 9f 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 0e 01 00 00 03 00 00 00 01 0f 30 00 00 01 01 00 03 00 00 00 01 0a 20 00 00 01 02 00 03 ... >
$node test.js

有关更多详细信息,请参阅。

我相信您正在从
axios.get()
方法中遇到编码问题。axios可以为您提供缓冲区,而不是从响应中创建缓冲区。尝试将您的请求更改为:

const fs = require("mz/fs");
const axios = require("axios");

const main = async () => {
  try {
    const attachImage = await axios.get(
      "https://d17fnq9dkz9hgj.cloudfront.net/uploads/2013/09/cat-black-superstitious-fcs-cat-myths-162286659.jpg",
      {
        responseType: 'arraybuffer',
        responseEncoding: 'binary'
      }
    );

    const { data } = attachImage;
    console.log(data);

    const testImg = await fs.readFile("./cat1.jpg", { encoding: null });
    console.log(testImg);


  } catch (err) {
    console.error(err);
  }
};

main();

当我在本地运行时,它们对我来说是相同的

$ node test.js
<Buffer ff d8 ff e1 0c 9f 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 0e 01 00 00 03 00 00 00 01 0f 30 00 00 01 01 00 03 00 00 00 01 0a 20 00 00 01 02 00 03 ... >
<Buffer ff d8 ff e1 0c 9f 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 0e 01 00 00 03 00 00 00 01 0f 30 00 00 01 01 00 03 00 00 00 01 0a 20 00 00 01 02 00 03 ... >
$node test.js
有关更多详细信息,请参阅