node.jsnext.jsformidable

formidable not saying file type to end of file uploaded


the following code, is uploading the file to the directory, however it is not adding the file extension to the created file. Nor is it sending back res.status(200).json({status:'success'});

However I do know that it is uploading them because I can see it in the folder public\files\f754121781049b2ef8557ef21 you can see they don't assign the extension file type.

import formidable from 'formidable';

export const config = {
  api: {
    bodyParser: false,
  },
};

export default async (req, res) => {
  const form = new formidable.IncomingForm();
  //const uploadFolder = path.join(__dirname, 'public','files');


  form.uploadDir = "public/files";
  form.keepExtensions = true;
  form.parse(req, (err, fields, files) => {
    //console.log(files.campaignAudio.newFilename);
    var file = files[0]

    try{
        const newFile = File.create({
            name:`files\${files.campaignAudio.newFilename}.mp3`
        });
        res.status(200).json({status:'success'});
    }
    catch(error){
        res.send(error);
    }
  });
};


Solution

  • form.keepExtensions = true;

    is deprecated syntax, instead pass in an object to formidable function with all the options inside.

    you are also not catching any errors, before your try catch ...

    Use something like

     if (err) {
     res
    return;
    }