node.js - How to create xlsx file from uploaded multipart content-type
问题描述
I encounter an issue saying FILE_ENDED
when i create xlsx file from the multipart content-type which is received to node.js server from front-end application.
Things done so far are as follows,
- I have registered the fastify-multipart in fastify as,
fastify.register(fastifyMultipart, {
attachFieldsToBody: true, sharedSchemaId: 'uploadFile',
limits: {
files: 1 // Max number of file fields
}
});
fastify.addContentTypeParser('multipart/form-data', (request, done) => {
done(null, request);
});
- Handling the file from controller's req.body,
function uploadFile(req, reply) {
const attrs = req.body;
upload(attrs)
.then(() => {
reply.code(200).send({ message: 'File uploaded successfully' });
})
.catch((error: FastifyError) => {
reply.send(error);
});
}
- Creating the uploaded file as xlxs format in upload.js file,
import util from 'util';
import { pipeline } from 'stream';
const pump = util.promisify(pipeline);
function upload(attrs) {
const { file } = attrs;
const { file: fileContent, mimetype: fileType } = file;
const fileName = `${new Date().getTime()}.xlsx`;
const filePath = `${__dirname}/${fileName}`;
await pump(fileContent, createWriteStream(filePath));
return Promise.resolve();
}
Expected the result to save the file and return success message.
Instead i received an error as follows,
{
errors: 'FILE_ENDED'
}
Any help is appreciated.
解决方案
推荐阅读
- python - 当给定 ID 前面没有一组值或后面没有其他组值时,删除 df 行
- c# - 连接两个数组时 DynamicExpressionParser 不起作用
- powershell - cmd中的“wmic memorychip get | clip”
- python - 如何包含 Pyinstaller 在生成 .exe 期间无法导入的文件
- python - Youtube Data API V3 请求的身份验证范围不足(Python)
- sql - 在oracle中将列值转换为列
- reactjs - 在 React Native 中创建 InputField 组件时出错
- javascript - 如何在单击一次按钮时呈现另一个反应组件?(功能组件)
- r - 如何在 Quanteda 中使用 docvars 从原始文本创建文档变量
- oracle - ORA-00920: 无效的关系运算符,即使没有括号错误