Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MEANJS : 413 (Request Entity Too Large)

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)
like image 635
Fabii Avatar asked Oct 09 '14 21:10

Fabii


People also ask

What does it mean 413 Request Entity Too Large?

The HTTP 413 Payload Too Large response status code indicates that the request entity is larger than limits defined by server; the server might close the connection or return a Retry-After header field.

How do I fix Nginx 413 Request Entity Too Large?

Error: 413 “Request Entity Too Large” in Nginx with “client_max_body_size” / Changes in Nginx config file. This is happening because of your nginx server not allow to upload file which is larger than defined size in nginx config file.To solve it , you have to modify your nginx configuration.


1 Answers

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!!!

like image 54
aarosil Avatar answered Oct 05 '22 17:10

aarosil