1

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');
        });
    });
cphill
  • 5,596
  • 16
  • 89
  • 182

1 Answers1

0

I'm not sure if you are trying to AJAX to send data but I could not get AJAX to send multi-form data correctly in a way that would make Multer happy. Here is what I use with Express and Multer. The following is some JQuery you can use to upload your form values to your server. Then you can manipulate your values and store them server side however you please.

<script language="JavaScript">
    $(document).ready(function () {
        $(".submitButton").click(function () {
            var values = {
                fields: $("#IDs").val()
            };
            var http = new XMLHttpRequest();
            http.open("POST", '/express/route', true);
            // stuff into a form
            var form = new FormData();
            form.append('values', JSON.stringify(values));
            // send data to server
            http.send(form);
        });
    });
</script>

Server side

app.post('/express/route', upload.array(), function (req, res, next) {
    console.log(req.body);
}

Now you have your (what I would call single form or non multi form) values server side.

I use Mongo not SQL at the moment so that's as far as I can help. I'm sure there are packages you can install, (I would search npmjs.com for sql), that will allow you to interface with SQL but Mongo seems to be popular right now with NodeJS. Here is a link I use for reference, when building a MEAN stack: http://www.dotnetcurry.com/nodejs/1032/nodejs-apps-in-visual-studio-mean-stackMEAN stack Also useful if you have Visual Studio and want to get NPM built into VS.

allegory
  • 124
  • 1
  • 1
  • 10