nodejs/express and binary data in POST

前端 未结 2 1380
我寻月下人不归
我寻月下人不归 2020-12-16 03:40

I\'m trying to send binary data to an express app. It works fine, as long as my values are smaller than 0x80. If a single value is 0x80 or greater, it messes up the whole b

相关标签:
2条回答
  • 2020-12-16 04:19

    After struggling with this for way too long, I came up with this solution:

    var express = require('express');
    var bodyParser = require('body-parser');
    var fs = require('fs');
    var path = require('path');
    
    app.use(bodyParser.raw({type: 'application/octet-stream', limit : '2mb'}))
    
    app.post('/nist-ws/rest/v1/nist/', function(req, res) {
        var filePath = path.join(__dirname, 'nist_received', `/${Date.now()}.nist`)
        fs.open(filePath, 'w', function(err, fd) {  
            fs.write(fd, req.body, 0, req.body.length, null, function(err) {
                if (err) throw 'error writing file: ' + err;
                fs.close(fd, function() {
                    console.log('wrote the file successfully');
                    res.status(200).end();
                });
            });
        });
    });
    

    bodyParser.raw fills req.body with a Buffer and the rest of the code is from: https://stackabuse.com/writing-to-files-in-node-js/

    0 讨论(0)
  • 2020-12-16 04:22

    It turned out to be an encoding issue. Everything seems to default to 'utf8', but in this case it needs to be set to 'binary'.

    For example:

    var buf = new Buffer(body.toString('binary'),'binary');
    

    Equally as important, I needed to set nodejs multiparty parser's encoding to 'binary'. See: https://github.com/superjoe30/node-multiparty/issues/37

    0 讨论(0)
提交回复
热议问题