我正在使用apollo-upload-client
上传文本文件。当文件到达服务器时,它以7bit
编码出现,例如:
const { filename, createReadStream, encoding } = await file;
console.log({ encoding });
--> { encoding: "7bit" }
然后创建如下流:
const stream = createReadStream({ encoding: "utf8" });
,然后使用以下函数为流输出字符串:
function streamToString(stream, cb) {
const chunks = [];
stream.on("data", (chunk) => {
chunks.push(chunk.toString());
});
stream.on("end", () => {
cb(chunks.join(""));
});
}
结果:
streamToString(stream, async (data) => {
console.log({data});
--> good string �� good string ��
}
这是保存在DBgood string �� good string ��
中的内容。
我想我需要用适当的编码将文件发送到服务器,也许是utf-8
。我该怎么做呢?
streamToString
函数实现不正确。下面是正确的版本:
const chunks = [];
readStream.on("data", function (chunk) {
chunks.push(chunk);
});
// Send the buffer or you can put it into a var
readStream.on("end", function () {
res.send(Buffer.concat(chunks));
});