0

我正在通过 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!');
});
})
4

1 回答 1

0

Changing the limits of the multer middleware didn't fix the issue,

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

but as per @adeneo's suggestion I gave bodyParser an other go and after changing the limit of urlencoded like so

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

I received all the data on the server and successfully saved the image.

于 2015-02-03T11:06:22.643 回答