MEANJS: Best way to send base64 image to server

Situation

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

Using AngularJS ahead of End; I have JSON with a base64 encoded image in a field.

What I need?

  • I want to send this JSON to the server ( NodeJS ).

I am using RESTful :

controller :

var article = new Articles ($scope.article); article.$save(function(response) { //If all is OK }, function(errorResponse) { //Error }); 

$scope.article has a field named " image " ($ scope.article.image) with a base in the image file.

service:

 (function() { 'use strict'; angular .module('articles') .factory('articles', ['$resource', function($resource) { return $resource('articles/:articleId', { articleId: '@_id' }, { update: { method: 'PUT' } }); } ]);})(); 

Problems

If JSON has no Base64 image in the field, it works fine ...

But...

If we add the Base64 line of the image to the field, the server response with this error :

  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 the request object is too large ... Image Size 84Kb !!!!!

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

  • How can I solve this server error?
  • What is the best way to send Base64 encoded image from Angular to Node?
  • Any suggestions?

Related answers:

I tried to do this, but it doesn’t work and I 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 !!!!!

Thanks!

+6
source share
1 answer

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 body was as expected if (!req.busboy) return next('route'); // or next(new Error('...')); // ... }); 

// ...

For more information, this private room .

0
source

Source: https://habr.com/ru/post/990008/


All Articles