1

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,

  1. 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);
  });
  1. 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);
    });
}
  1. 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.

4

0 回答 0