13

I am using a MEANJS stack, I upload an image using ng-flow and save the imgsrc as base64 url.

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARkAAACzCAYAAAC94GgrA....

Here is my mongoose schema:

var ServiceSchema = new  mongoose.Schema({
    name : String,
    url: String,
    description : String,
    category : String,
    imgsrc: String
});

I run into a Request Entity Too Large server error for large images.

I could resize the image prior to upload but this still only allows me image of size 200 x 200

$scope.resizeimageforupload = function(img){
        var canvas = document.getElementById('canvas');

        var MAX_WIDTH = 200; //400; too big still
        var MAX_HEIGHT = 200; //300 too big still
        var width = img.width;
        var height = img.height;

        if (width > height) {
          if (width > MAX_WIDTH) {
            height *= MAX_WIDTH / width;
            width = MAX_WIDTH;
          }
        } else {
          if (height > MAX_HEIGHT) {
            width *= MAX_HEIGHT / height;
            height = MAX_HEIGHT;
          }
        }
        canvas.width = width;
        canvas.height = height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, width, height);

        var dataURL = canvas.toDataURL("image/png");
        dataURL.replace(/^data:image\/(png|jpg);base64,/, "");

        return dataURL;
    };

Any ideas on a work around or alternate solution ?

request entity too large: 413

Error: request entity too large
    at makeError (Angular\expresstest\node_modules\body-parser\node_modules\raw-body\index.js:184:15)
    at module.exports (Angular\expresstest\node_modules\body-parser\node_modules\raw-body\index.js:40:15)
    at read (Angular\expresstest\node_modules\body-parser\lib\read.js:62:3)
    at jsonParser (Angular\expresstest\node_modules\body-parser\lib\types\json.js:87:5)
    at Layer.handle [as handle_request] (Angular\expresstest\node_modules\express\lib\router\layer.js:76:5)
    at trim_prefix (Angular\expresstest\node_modules\express\lib\router\index.js:270:13)
    at Angular\expresstest\node_modules\express\lib\router\index.js:237:9
    at Function.proto.process_params (Angular\expresstest\node_modules\express\lib\router\index.js:312:12)
    at Angular\expresstest\node_modules\express\lib\router\index.js:228:12
    at Function.match_layer (Angular\expresstest\node_modules\express\lib\router\index.js:295:3)
Fabii
  • 3,820
  • 14
  • 51
  • 92

3 Answers3

32

You can add following to express config:

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

Basically you need to configure Express webserver to accept bigger request size. Replace 50mb with whatever maxsize you want to accept.

Hope this helps!!!

aarosil
  • 4,848
  • 3
  • 27
  • 41
  • 1
    Worked great!; not sure what this was for : app.use(methodOverride({limit: '50mb'})); but it was undefined. – Fabii Oct 09 '14 at 21:38
  • @aarosil: Is `methodOverride` not needed at all then? Could you remove it from your answer if that's the case? – Chris Foster Nov 26 '14 at 20:47
  • This doesn't work for me. I added it but I keep getting the error that is in the OP. – reaper_unique Jan 31 '16 at 20:06
  • It seems like at some point after a customer provides his login using passport-local, raw-body is being loaded which has a limit size of 100 Kb. – reaper_unique Jan 31 '16 at 21:09
  • @reaper_unique this is exactly my case too. What's the soln here? – lonelymo Feb 14 '16 at 15:38
  • Unfortunately I haven't found the cause yet but I have a bigger issue. I forgot to check with my host if they support node.js, which they don't and my client doesn't wish to move to a more expensive solution which does support node.js so I have to migrate my project asap back to .NET. – reaper_unique Feb 16 '16 at 15:37
  • what is app.use here? – Arj 1411 Nov 09 '16 at 12:40
0

2016, there may have been changes, i needed to set the 'type' in addition to the 'limit' for bodyparser, example: var bodyParser = require('body-parser');

  var app = express();
  var jsonParser       = bodyParser.json({limit:1024*1024*20, type:'application/json'});
  var urlencodedParser = bodyParser.urlencoded({ extended:true,limit:1024*1024*20,type:'application/x-www-form-urlencoding' })

  app.use(jsonParser);
  app.use(urlencodedParser);
user1709076
  • 2,538
  • 9
  • 38
  • 59
0

Recently, I confronted with this issue and I just added app.use(bodyParser.json({ limit: "50mb" })); this one line in my express app, it just worked :)

And also one more thing don't forget to add above code line before app.use(bodyParser.json()) this otherwise, it won't work.

Whatever you set values regarding bodyparser you've to do before the above code line.

Because, express will go back to its default values and your change won't get effect. I hope this might help :)

Vivek S
  • 179
  • 1
  • 2
  • 17