So req.files can be an array. Try making console.log(req.files) in the handler and see how it looks ... But I think you better just do it, as in your example. You can make it work by simply removing ../test/common require at the top and changing the constants to static values (for example, use __dirname + '/uploads' instead of TEST_TMP ).
Handler:
var form = new formidable.IncomingForm(), files = [], fields = []; form.uploadDir = __dirname + '/uploads'; form .on('field', function(field, value) { console.log(field, value); fields.push([field, value]); }) .on('file', function(field, file) { console.log(field, file); files.push([field, file]); }) .on('end', function() { console.log('-> upload done'); res.writeHead(200, {'content-type': 'text/plain'}); res.write('received fields:\n\n '+util.inspect(fields)); res.write('\n\n'); res.end('received files:\n\n '+util.inspect(files)); }); form.parse(req);
Now the files are in __dirname + '/uploads' , named as the output on the console (and the answer, as you can see in the end handler).
rfunduk
source share