Search code examples
javascriptnode.jsexpressxmlhttprequestmultipartform-data

Express 4 FormData multipart parse POST request


I'm trying to save incoming file from FormData xhr request but i can't even parse incoming request. This is how i'm trying to send file:

...
var formData = new FormData();
formData.append(fileType + '-blob', blob);
var request = new XMLHttpRequest();
request.open('POST', url);
request.send(data);
...

And this is how i'm trying to catch it:

var express = require('express');
var router = express.Router();

router.post('/savestream', function(req, res) {
    var body = '';
    req.on('readable', function() {
        body += req.read();
    });
    req.on('end', function() {
        //body = JSON.parse(body);
        console.log(body);
        res.end(body);
    });
});

I'm also using bodyParser in my app:

var bodyParser = require('body-parser');
...
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
...

But when i'm trying to receive it, i'm getting raw data like:

------WebKitFormBoundaryB0wkHt33s0gbqiB3
Content-Disposition: form-data; name="video-blob"; filename="blob"
Content-Type: video/webm

Eߣ@ B��B��B��B�B�@webmB��B��S�g )I�f@(*ױ@B@M�@whammyWA@whammyD�@6T�k@3�@0ׁcŁ��"��@und�@V_VP8%��@VP8����@@���C�u@����@���P�*@�>m6�I�#"� (�in�wa@    ��l����9}�r�d�=���{퓐��'!��NC�l����9}�r�d�=���{퓐��'!��NC�l����9}�r�d�=���{퓐��'!��NC�l����9}�r�d�=���{퓐��'!��NC�l�����
------WebKitFormBoundaryB0wkHt33s0gbqiB3--

How can i parse it? When i send json data it works well.


Solution

  • The body-parser module currently does not a provide a multipart/form-data parser. For that you will need something like multer, busboy/connect-busboy, multiparty, or formidable.