【问题标题】:Node js request entity too large with multer uploadNode js请求实体太大,带有multer上传
【发布时间】:2016-04-07 19:22:18
【问题描述】:

好吧,我尝试了不同的方法来上传 200k 的文件,增加了更改参数的限制,我所做的一切都改变了 multer。 Fucei 我所知道的我在堆栈中读到的所有内容,我在 google 上找到了基本的 google 搜索完成了我的问题,而不是向上而是向下,图片就像一个魅力。服务器支持上传图片我 atualemente 使用 php 执行此任务,但如果不使用 nodejs,我将在 php 中执行相同的文件。感谢收听。

我的代码

var express = require('express');
var app = express();
var http = require('http').Server(app);
var bodyParser = require("body-parser");
var fs = require('fs');
var multer = require('multer');



var destino = '/var/www/upload/';
var upload = multer({dest: 'uploads/',
    rename: function (fieldname, filename) {
        return  filename + Date.now();
    },
    limits: {
        fieldNameSize: 200,
        files: 5,
        fields: 5
    }
});

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());




app.post('/image', upload.single('message'), function (req, res) {


    var file = req.file;
    fs = require('fs')
    fs.readFile(file.path, 'base64', function (err, data) {
        if (err) {
            return console.log(err);
        }

/* do something if data*/
        fs.unlinkSync(file.path);
    });

    });

http.listen(8080, function () {
    console.log("Conectado e escutando na porta: 8080");


});

向我显示的错误就是它:错误:请求实体太大

        at readStream (/var/www/likeyounode/node_modules/body-parser/node_modules/raw-body/index.js:196:17) 
        at getRawBody (/var/www/likeyounode/node_modules/body-parser/node_modules/raw-body/index.js:106:12) 
        at read (/var/www/likeyounode/node_modules/body-parser/lib/read.js:76:3) 
        at urlencodedParser (/var/www/likeyounode/node_modules/body-parser/lib/types/urlencoded.js:109:5) 
        at Layer.handle [as handle_request] (/var/www/likeyounode/node_modules/express/lib/router/layer.js:95:5) 
        at trim_prefix (/var/www/likeyounode/node_modules/express/lib/router/index.js:312:13)
        at /var/www/likeyounode/node_modules/express/lib/router/index.js:280:7
        at Function.process_params (/var/www/likeyounode/node_modules/express/lib/router/index.js:330:12)
        at next (/var/www/likeyounode/node_modules/express/lib/router/index.js:271:10)
        at expressInit (/var/www/likeyounode/node_modules/express/lib/middleware/init.js:33:5)

我查看了我所有的代码,并且只需要大量的证据来证明我想要的更多,这将揭示我的大部分项目。

我在正文中进行了一些配置,如果您注意到它们在内容中进行了说明,那么您可能会怀疑不会让您运行自己的应用程序的所有功能。

【问题讨论】:

  • 您似乎没有设置任何 multer 配置,而且您的代码似乎不完整。
  • 您尝试过我的解决方案吗? :)

标签: javascript node.js


【解决方案1】:

在我的情况下,设置限制@ node.js (webapp)

var express = require('express'); var app = express(); 
var bodyParser = require('body-parser');            
app.use(bodyParser.json({limit:'50mb'})); 
app.use(bodyParser.urlencoded({extended:true, limit:'50mb'})); 

我也(最重要的)不得不设置限制@nginx(webapp反向代理)

 # Max upload size.
client_max_body_size 50M;

【讨论】:

  • 谢谢,EMX 只是为了确保有关 client_max_body_size 50M 的更多细节;必须在 /etc/nginx/nginx.conf 文件中添加这一行。
  • 对我来说,只需将一行添加到 nginx 即可解决问题。这是文档:nginx.org/en/docs/http/…
【解决方案2】:

我无法通过您的所有代码来判断,但仅通过查看错误,您应该能够使用以下内容设置正文帖子大小:

https://www.npmjs.com/package/multer#limits

 multer({
    storage: storage,
    limits: { fileSize: maxSize }
 })

如果这不能解决您的问题,bodyParser 也有限制。 可以找到它的选项:

https://github.com/expressjs/body-parser#limit

bodyParser({limit: '4MB'});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    相关资源
    最近更新 更多