如何从 Buffer.from 获得与 fs.readFile 相同的输出?
How to get the same output from Buffer.from as fs.readFile?
我正在为我的 slackbot 开发一项功能,将二进制数据的 post 请求从图像发送到名为 servicenow 的票务系统,这让 Nodejs 获得了一些正确的输出。我使用 axios 从 url 获取图像,然后将数据发送到 Buffer.from 并使用二进制,这没有给我所需的正确输出。然而,使用 fs.readFile 似乎也 return 一个 Buffer 对象,这是我需要的输出。
fs.readFile says that encoding is set to null for readFile. The docs for Buffer.from 的文档说默认编码是 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
<Buffer fd fd fd fd 0c fd 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 ... 108862 more bytes>
<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 ... 114344 more bytes>
我认为您 运行 是 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 ... >
有关详细信息,请参阅 axios.get()
options documentation。
我正在为我的 slackbot 开发一项功能,将二进制数据的 post 请求从图像发送到名为 servicenow 的票务系统,这让 Nodejs 获得了一些正确的输出。我使用 axios 从 url 获取图像,然后将数据发送到 Buffer.from 并使用二进制,这没有给我所需的正确输出。然而,使用 fs.readFile 似乎也 return 一个 Buffer 对象,这是我需要的输出。
fs.readFile says that encoding is set to null for readFile. The docs for Buffer.from 的文档说默认编码是 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
<Buffer fd fd fd fd 0c fd 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 ... 108862 more bytes>
<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 ... 114344 more bytes>
我认为您 运行 是 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 ... >
有关详细信息,请参阅 axios.get()
options documentation。