How to disable Express BodyParser for file uploads (Node.js)

后端 未结 7 1893
深忆病人
深忆病人 2020-11-30 18:26

This seems like it should be a fairly simple question, but I\'m having a really hard time figuring out how to approach it.

I\'m using Node.js + Express to build a we

7条回答
  •  孤独总比滥情好
    2020-11-30 19:06

    Within Express 3, you can pass parameter to the bodyParser as {defer: true} - which in term defers multipart processing and exposes the Formidable form object as req.form. Meaning your code can be:

    ...
    app.use(express.bodyParser({defer: true}));
    
    ...
    // your upload handling request 
    app.post('/upload', function(req, res)) {
        var incomingForm = req.form  // it is Formidable form object
    
        incomingForm.on('error', function(err){
    
              console.log(error);  //handle the error
    
        })
    
        incomingForm.on('fileBegin', function(name, file){
    
             // do your things here when upload starts
        })
    
    
        incomingForm.on('end', function(){
    
             // do stuff after file upload
        });
    
        // Main entry for parsing the files
        // needed to start Formidables activity
        incomingForm.parse(req, function(err, fields, files){
    
    
        })
    }
    

    For more detailed formidable event handling refer to https://github.com/felixge/node-formidable

提交回复
热议问题