I'm facing what I believe is a common problem with using enctype="multipart/form-data"
form types with ExpressJS. Without middleware, req.body
(or bodyParser) does not handle this form type and as a result I am sending null
values for my fields on POST
requests. My form has a file upload section, which is why I'm using multipart
and I'm using the multer
and multer-s3
modules to handle the image uploads to an s3 bucket, but not sure how to use multer
or some other module to help me store the field values to my Mysql (Sequelize ORM) database. Can any one provide guidance as to if multer
is the right module to use for this type of form upload and point me to documentation where I can swap out req.body
with some module specific method?
What my form looks like
<form action="/app/post/create" method="post" enctype="multipart/form-data">
<label for="discovery">Discovery:</label>
<textarea id="discovery-text-field" name="discovery"></textarea>
</br>
<label for="report-link">Link to Report:</label>
<input type="textarea" name="reportLink">
<br />
<label for="file-attachment">File Attachment:</label>
<input type="file" id="file-input" name="fileUpload[]" multiple>
</form>
Routing:
appRoutes.route('app/post/create')
.post(function(req, res){
models.Post.create({
discovery: req.body.discovery,
reportLink: req.body.reportLink,
userId: req.user.userId
}).then(function(){
req.flash('info', 'Post was successfully created.');
res.redirect('/app');
});
});