由具有multer中间件的Express服务器接收的发布数据将被截断

时间:2015-02-03 08:52:34

标签: javascript node.js post express multer

我通过POST请求向我的Express服务器发送1.5MB字符串,但是当我查看收到的数据时,它只有786 KB长。 我发送的字符串是base64图像字符串。我也试图改变multer的限制无济于事。

客户端:

function upload(file) {
var form = new FormData(),
    xhr = new XMLHttpRequest();
form.append('filename', "imageName.jpg");
form.append('imageData', file);
xhr.open('post', 'http://server/imgdata', true);
xhr.send(form);
}

服务器

app.use(multer({
dest: './uploads/',
// WILL RENAME THE RECEIVED FILE
rename: function(fieldname, filename) {
    return filename + Date.now();
},
// UPLOAD HAS STARTED
onFileUploadStart: function(file) {
    console.log(file.originalname + ' is starting ...')
},
// FILE HAS BEEN RECEIVED AND SAVED
onFileUploadComplete: function(file) {
    console.log(file.fieldname + ' uploaded to  ' + file.path)
    done = true;
},
onFieldsLimit: function() {
    console.log('Crossed fields limit!')
}
 }));
app.post('/imgdata', function(req, res) {

// THIS IS RETURNS ONLY A PART OF THE DATA
res.json(req.body.image2);

var data = req.body.image2.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');

// THE IMAGE IS SAVED BUT ONLY THE 786KB OF IT EVERY TIME REGARDLESS     
// OF THE SIZE OF DATA SEND 
fs.writeFile('image.jpg', buf, function(err) {
    if (err) throw err;
    console.log('It\'s saved!');
});
})

1 个答案:

答案 0 :(得分:0)

更改multer中间件的限制并没有解决问题,

limits: {
    fieldNameSize : 100000, 
    fieldSize : 5242880 
},

但是根据@ adeneo的建议,我给了bodyParser一个额外的去,并且在更改了urlencoded的限制之后就这样了

app.use(bodyParser.urlencoded({
extended: true,
limit: 100000000 
}));

我收到了服务器上的所有数据并成功保存了图片。