使用apollo-upload-client时如何在上传文件中设置utf-8编码?
How to set utf-8 encoding in the uploading file when using apollo-upload-client?
我正在使用 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 ��
}
这是保存在数据库中的内容 good 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));
});
我正在使用 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 ��
}
这是保存在数据库中的内容 good 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));
});