情况
我正在使用MEAN.JS框架(MongoDB,ExpressJS,AngularJS和NodeJS)。
在前端使用AngularJS;我有一个包含base64编码的图像的JSON字段。
我想要什么?
- 我想将此JSON发送到服务器(NodeJS)。
我正在使用RESTful:
控制器:
var article = new Articles ($scope.article);
article.$save(function(response) {
//If all is OK
}, function(errorResponse) {
//Error
});
$scope.article有一个名为"image"的字段($scope.article.image),其中包含图像的base64字符串。
服务:
(function() {
'use strict';
angular
.module('articles')
.factory('articles', ['$resource',
function($resource) {
return $resource('articles/:articleId', { articleId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);})();
问题
如果JSON中没有任何字段包含Base64图像,则正常工作...
但是...
如果我们在一个字段中添加了图像的Base64字符串,服务器响应将出现此错误:
Error: request entity too large at makeError (/Users/aralroca/Documents/Aral/Projecte/node_modules/body-parser/node_modules/raw-body/index.js:184:15)
at module.exports (/Users/aralroca/Documents/Aral/Projecte/node_modules/body-parser/node_modules/raw-body/index.js:40:15)
at read (/Users/aralroca/Documents/Aral/Projecte/node_modules/body-parser/lib/read.js:62:3)
at jsonParser (/Users/aralroca/Documents/Aral/Projecte/node_modules/body-parser/lib/types/json.js:96:5)
at Layer.handle [as handle_request] (/Users/aralroca/Documents/Aral/Projecte/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/Users/aralroca/Documents/Aral/Projecte/node_modules/express/lib/router/index.js:269:13)
at /Users/aralroca/Documents/Aral/Projecte/node_modules/express/lib/router/index.js:236:9
at Function.proto.process_params (/Users/aralroca/Documents/Aral/Projecte/node_modules/express/lib/router/index.js:311:12)
at /Users/aralroca/Documents/Aral/Projecte/node_modules/express/lib/router/index.js:227:12
at Function.match_layer (/Users/aralroca/Documents/Aral/Projecte/node_modules/express/lib/router/index.js:294:3)
提示请求实体过大... 图像大小为84Kb!!!!!
(我尝试使用$http资源,但结果相同...)
- 如何解决此服务器错误?
- 从Angular发送Base64编码图像到Node的最佳方法是什么?
- 有什么建议吗?
相关答案:
我尝试了这个但不起作用,也不理解:
app.use(bodyParser.urlencoded({limit: '50mb'}));
app.use(bodyParser.json({limit: '50mb'}));
已过时,Base64图像的大小为84kb!!!
谢谢!
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
和app.use(bodyParser.json({limit: '50mb'}));
它们都可以正常工作。 - pgrodrigues