Significance of form analysis with a formidable file name

I am using formable to handle file uploads in NodeJ. I am a bit stuck with the parsing field values.

How do I get the project_id value for a form handler so I can write the parameter in my name?

<input type="text" id="project_id" value="{{projects._id}}" readonly>



To be more specific, here's a detailed overview of my form loading handling:'/uploads/', function (req, res){
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        res.writeHead(200, {'content-type': 'image/jpeg'});
        res.write('received upload: \n\n');
        var project = fields.project_id;
        res.end(util.inspect(project, {fields: fields, files: files}));

    form.on('end', function(project, fields, files){ 
        /*Temporary location of our uploaded file */
        var temp_path = this.openedFiles[0].path;
        /*The file name of the uploaded file */
        var file_name =  project + '.' + this.openedFiles[0].name;


I can register var project

in part form.parse

. But I am not getting the variable in the part form.on('end'...


HTML form

<form   id="uploadForm"
    <input type="text" name="project_id" id="project_id" value="{{projects._id}}" readonly>
    <input multiple="multiple" type="file" name="upload" />
    <button type="submit">Upload</button>



source to share

2 answers

The fancy end

callback takes no parameters, but I'm not sure if you even need to call it if you are using a callback parse

. I think you are looking for something like this:

var fs = require('fs');'/uploads', function(req, res, next) {
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
        if (err) next(err);

        // TODO: make sure my_file and project_id exist    
        fs.rename(files.my_file.path, fields.project_id, function(err) {
            if (err) next(err);


You will need to listen for the event end()

if you choose not to use a callback parse

, for example:

new formidable.IncomingForm().parse(req)
    .on('file', function(name, file) {
        console.log('Got file:', name);
    .on('field', function(name, field) {
        console.log('Got a field:', name);
    .on('error', function(err) {
    .on('end', function() {




Client side script:

    //Upload the file
    var fd = new FormData();
    //Take the first selected file
    fd.append("dbDocPath", 'invoices/' +;
    fd.append("file", file);
            method: 'POST',
            url: $rootScope.apiUrl + 'uploadDocToServer',
            data: fd,
            headers: {
                'Content-Type': undefined
            //prevents serializing payload.  don't do it.
            transformRequest: angular.identity,
        }).success(function (response) {
           if (response.success) {


Server side script:

var fileDir = path.join(__dirname, '/../uploads');

// create an incoming form object
var form = new formidable.IncomingForm();
var dbDocPath = '';
        .on('field', function (name, field) {
            //console.log('Got a field:', field);
            //console.log('Got a field name:', name);
            dbDocPath = field;
        .on('file', function (name, file) {
            //console.log('Got file:', name);

            // specify that we want to allow the user to upload multiple files in a single request
            //form.multiples = true;

            // store all uploads in the /uploads directory
            form.uploadDir = fileDir;

            fs.rename(file.path, path.join(form.uploadDir,;

            // every time a file has been uploaded successfully,
            // rename it to it orignal name

            var bucket = new AWS.S3();

            var params = {
                Bucket: DocsConfig.bucketName,
                Key: dbDocPath,
                Body: fs.createReadStream(path.join(form.uploadDir,,
                ACL: 'public-read'

            bucket.putObject(params, function (perr, pres) {
                if (perr) {
                    //console.log("Error uploading data: ", perr);
                } else {
                    //console.log("Successfully uploaded data", pres);
        .on('error', function (err) {
            res.send({'success': false, error: err});
        .on('end', function () {
            res.send({'success': true});
// parse the incoming request containing the form data


Just remember that the sequence of sending parameters to formData () must be the same as in the above code, since the file upload path is required to upload to fate.



All Articles