【发布时间】:2015-09-17 06:40:58
【问题描述】:
情况
我正在使用MEAN.JS 框架(MongoDB、ExpressJS、AngularJS 和 NodeJS)。
在前端使用 AngularJS;我有一个 JSON,字段中有一个 base64 编码的 image。
我想要什么?
- 我想将此 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 resource 并发生同样的情况......)
- 如何解决此服务器错误?
- 从 Angular 向 Node 发送 Base64 编码图像的最佳方式是什么?
- 有什么建议吗?
相关答案:
我试过这样做,但不工作,不明白:
app.use(bodyParser.urlencoded({limit: '50mb'}));
app.use(bodyParser.json({limit: '50mb'}));
bodyParser 已弃用,Base64 图像的大小为 84kb!!!!!
谢谢!!
【问题讨论】:
-
应用程序是否在代理后面?在这种情况下,也要检查代理配置。
-
试试这个private npm module。
-
我还通过 post 请求发送了一个 base64 图像,并且在大小方面也遇到了一些问题。我的快速配置中有:
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));和app.use(bodyParser.json({limit: '50mb'}));,它工作正常。 -
mmm 现在这个解决方案不起作用,因为 bodyParser 现在已被弃用......另一种方式,我不明白为什么会发生这种情况......默认限制为 1MB,但大小base64 图像只有 84kb ......也许有一个类似于 bodyParser.urlencoded 的代码并且 bodyParser.json 不被弃用?谢谢!
-
@sam100rav 我尝试使用 base64-image npm 模块并出现同样的问题!!错误:请求实体太大...
标签: angularjs node.js mongodb base64 meanjs