0
app.post('/upload', function (req, res) {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      try{
        if (files.file.name != '') {
          file_newname = dt.MD5(files.file.name + Date() + Math.random()) + '.jpg' + ;
          var file_newpath = './tmp/' + file_newname;
          fs.readFile(file_oldpath, function (err, data) {

            // Write the file
            fs.writeFile(file_newpath, data, function (err) {
              console.log('File written!');
              res.end(JSON.stringify({             
                message: 'file uploaded successfully'
              }));
            });            

          });
        }
      }catch (e) {

      }
    });
  });

The single image upload is working perfectly.I tried the following code

var form = new formidable.IncomingForm();



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);

But only a single image gets uploaded. i m using react in frontend. Node and express in backend.

I also tried multer. But that doesnt working

app.post('/getrast', upload.array('files'), function (req, res) {

     res.json({data: req.files});
     });
5
  • are you able to select multiple files ? Commented Jan 13, 2020 at 9:59
  • yes i provided multiple in input Commented Jan 13, 2020 at 10:35
  • could you please try form.multiles = true ? like this, github.com/juhilsomaiya/node-python-project/blob/master/… Commented Jan 13, 2020 at 11:06
  • thanks its working. but the file is created without extensio. where we append the extension? And is there a way to give response of array of filenames uploaded Commented Jan 13, 2020 at 11:16
  • Pls approve and upvote answer. Yes you can definitely rename your files using .rename function use it with form.on('file') as one file will be added, you can rename it check this github.com/juhilsomaiya/node-python-project/blob/master/… Commented Jan 13, 2020 at 11:21

1 Answer 1

2

Use the multiple flag with the incoming form with true as value.

var form = new formidable.IncomingForm();

form.multiples = true; //use this while dealing with multiple files

files = [],
fields = [];
form.on('field', function(field, value) {
    fields.push([field, value]);
})
form.on('file', function(field, file) {

    fs.rename('add your logic here for renaming files'); // rename it here

    console.log(file.name);
    files.push([field, file]);
})
form.on('end', function() {
    console.log('done');
    //res.redirect('/forms');
});
form.parse(req);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.