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

时间:2022-12-02 21:47:39

Situation

I'm using MEAN.JS framework (MongoDB, ExpressJS, AngularJS and NodeJS).

我正在使用MEAN.JS框架(MongoDB,ExpressJS,AngularJS和NodeJS)。

Using AngularJS in frontEnd; I have a JSON with a base64 encoded image in a field.

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

What I want?

  • I want to send this JSON to the server (NodeJS).
  • 我想将此JSON发送到服务器(NodeJS)。

I'm using RESTful:

我正在使用RESTful:

controller:

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


article.$save(function(response) {
    //If all is OK
}, function(errorResponse) {
    //Error 
 });

$scope.article have a field named "image" ($scope.article.image) with the base64 string of the image.

$ scope.article有一个名为“image”($ scope.article.image)的字段,其中包含图像的base64字符串。

service:

(function() {
'use strict';

angular
    .module('articles')
    .factory('articles', ['$resource',

    function($resource) {
        return $resource('articles/:articleId', { articleId: '@_id'
        }, {
            update: {
                method: 'PUT'
            }
        });
    }
]);})();

Problems

If the JSON don't have any Base64 Image in a field works fine...

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

But...

If we add the Base64 String of the Image in a field the server response with this error:

如果我们在字段中添加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)

Say that the request entity is too large... The image size is 84Kb!!!!!

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

(I tried with $http resource and occurs the same...)

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

  • How can I solve this server error?
  • 如何解决此服务器错误?

  • What is the best way to send from Angular to Node a Base64 encoded image?
  • 从Angular发送到Node64 Base64编码图像的最佳方法是什么?

  • Any suggestions?

Relationed answers:

I tried to do this but don't work and don't understand:

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

 app.use(bodyParser.urlencoded({limit: '50mb'}));
 app.use(bodyParser.json({limit: '50mb'}));

bodyParser is deprecated and the size of the Base64 Image is 84kb!!!!!

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

Thank you!!

1 个解决方案

#1


0  

Try using :

尝试使用:

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('...'));

    // ...
});

// ...

For more info checkout this private npm.

有关更多信息,请查看此私人npm。

#1


0  

Try using :

尝试使用:

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('...'));

    // ...
});

// ...

For more info checkout this private npm.

有关更多信息,请查看此私人npm。