简体   繁体   English

MEANJS:413(请求实体太大)

[英]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.我正在使用 MEANJS 堆栈,我使用ng-flow上传图像并将 imgsrc 保存为 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我可以在上传之前调整图像大小,但这仍然只允许我使用 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请求实体太大: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)

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.基本上你需要配置 Express webserver 来接受更大的请求大小。 Replace 50mb with whatever maxsize you want to accept.用您想要接受的任何 maxsize 替换50mb

Hope this helps!!!希望这可以帮助!!!

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'); 2016 年,可能发生了变化,除了 bodyparser 的“限制”之外,我还需要设置“类型”,例如: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);

Recently, I confronted with this issue and I just added app.use(bodyParser.json({ limit: "50mb" }));最近,我遇到了这个问题,我刚刚添加了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.还有一件事不要忘记在app.use(bodyParser.json())之前添加上面的代码行,否则,它将无法工作。

Whatever you set values regarding bodyparser you've to do before the above code line.无论您为 bodyparser 设置什么值,您都必须在上述代码行之前执行。

Because, express will go back to its default values and your change won't get effect.因为,express 会恢复到它的默认值,你的更改不会生效。 I hope this might help :)我希望这可能会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM