MEANJS:将base64映像发送到服务器的最佳方式

Ara*_*oca 6 base64 mongodb node.js angularjs meanjs

情况

我正在使用MEAN.JS框架(MongoDB,ExpressJS,AngularJSNodeJS).

在frontEnd中使用AngularJS ; 我在一个字段中有一个带有base64编码图像JSON.

我想要的是?

  • 我想将此JSON发送到服务器(NodeJS).

我正在使用RESTful:

控制器:

var article = new Articles ($scope.article);


article.$save(function(response) {
    //If all is OK
}, function(errorResponse) {
    //Error 
 });
Run Code Online (Sandbox Code Playgroud)

$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'
            }
        });
    }
]);})();
Run Code Online (Sandbox Code Playgroud)

问题

如果JSON在一个字段中没有任何Base64图像工作正常...

但...

如果我们在字段中添加Image的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)
Run Code Online (Sandbox Code Playgroud)

假设请求实体太大 ......图像大小为84Kb !!!!!

(我尝试使用$ http资源并发生相同的...)

  • 如何解决此服务器错误?
  • 从Angular发送到Node64 Base64编码图像的最佳方法是什么?
  • 有什么建议?

相关答案:

我试图这样做,但不工作,不明白:

 app.use(bodyParser.urlencoded({limit: '50mb'}));
 app.use(bodyParser.json({limit: '50mb'}));
Run Code Online (Sandbox Code Playgroud)

不推荐使用bodyParser,Base64图像的大小为84kb !!!!!

谢谢!!

sam*_*rav 0

尝试使用:

var express = require('express');
var app = express();
var busboy = require('connect-busboy');

app.post("/url_path",
busboy({
    limits: {
        fileSize: 10 * 1024 * 1024
    }
}),
function(req, res, next) {
    // check that the request's body was as expected
    if (!req.busboy) return next('route'); // or next(new Error('...'));

    // ...
});
Run Code Online (Sandbox Code Playgroud)

// ...

欲了解更多信息,请查看这个私人 npm