Nithin M D
Nithin M D

Reputation: 124

formidable saves file without extension in Node js

    app.post('/file_upload', function (req, res) {    

    var form = new formidable.IncomingForm();
    form.uploadDir = path.join(__dirname, '/uploads');    
    files = [],
    fields = [];
    form.on('field', function(field, value) {
        fields.push([field, value]);
    })
    form.on('file', function(field, file) {
        console.log(file.name);
        files.push([field, file]);
    })
    form.on('end', function() {
        console.log('done');
       // res.redirect('/forms');
    });
    form.parse(req);
});

UI used to upload multiple images in nodejs using formidable

The files are being creating. But the images are getting saved without extension.

enter image description here

Is there anything extra steps i have to do?

Upvotes: 1

Views: 1879

Answers (2)

Nithin M D
Nithin M D

Reputation: 124

This worked for me:

form.keepExtensions = true;

Upvotes: 4

Ali Mousavi
Ali Mousavi

Reputation: 915

Try onPart:

form.on('part', function (part) {
  if (part.filename) {
    if (!isInvalidFileName(part.filename) || !isInvalidMimeType(part.mime)) {
      files.push([part]);
    }
  }
})

Upvotes: 0

Related Questions