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