Parse form value with formidable to filename

后端 未结 2 877
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 11:10

I´m using formidable to handle my file uploads in NodeJs. I´m a little stuck at parsing field values.

How do I get the value of project_id to the form handler, so I can

相关标签:
2条回答
  • 2021-02-06 11:25

    Client side script:

        //Upload the file
        var fd = new FormData();
        //Take the first selected file
        fd.append("dbDocPath", 'invoices/' + file.name);
        fd.append("file", file);
        $http({
                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 = '';
    form.parse(req)
            .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, file.name));
    
                // every time a file has been uploaded successfully,
                // rename it to it's orignal name
    
                var bucket = new AWS.S3();
                //console.log(dbDocPath);
    
                var params = {
                    Bucket: DocsConfig.bucketName,
                    Key: dbDocPath,
                    Body: fs.createReadStream(path.join(form.uploadDir, file.name)),
                    ACL: 'public-read'
                };
    
                bucket.putObject(params, function (perr, pres) {
                    if (perr) {
                        //console.log("Error uploading data: ", perr);
                    } else {
                        fs.unlinkSync(path.join(form.uploadDir, file.name));
                        //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
    //form.parse(req);
    

    Just keep one thing in mind that the sequence of sending parameters to formData() should be same as mentioned in above code as file upload needs path to upload to the destiny.

    0 讨论(0)
  • 2021-02-06 11:30

    Formidable's end callback doesn't take any parameters, but I'm not sure you even need to call it if you're using the parse callback. I think what you're looking for is something like this:

    var fs = require('fs');
    app.post('/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);
                res.end();
            });
        });
    });
    

    You would need to listen for the end() event if you chose not to use the parse callback, like this:

    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) {
            next(err);
        })
        .on('end', function() {
            res.end();
        });
    
    0 讨论(0)
提交回复
热议问题