【问题标题】:Multer Nodejs Large File Upload - HTTP error 413 in Production environmentMulter Nodejs大文件上传 - 生产环境中的HTTP错误413
【发布时间】:2021-09-26 22:14:14
【问题描述】:

我正在使用 Multer 将文件上传到服务器的 Nodejs 项目。该应用程序在 Azure 上作为“基本 (B1)”定价层下的 Web 应用程序托管。
该应用程序在本地开发环境中的任何文件大小都可以正常运行。

但在生产环境中,文件大小大于 25MB(大约),应用程序崩溃并显示以下错误:

此页面目前无法使用。
如果问题仍然存在,请联系网站所有者。
HTTP 错误 413

我已按照following question 中提供的建议为 multer 添加限制,但这似乎也不起作用。

下面是我的代码:

const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs-extra') // (commented below) - delete highlighted file from server
const open = require('open');
const app = express();

// Receiving HTTP ERROR 413 for large files - solution: https://stackoverflow.com/a/40890833/6908282 , another reference: https://stackoverflow.com/a/61079051/6908282
// bodyParser deprecated: https://stackoverflow.com/a/24344756/6908282
// var bodyParser = require('body-parser');
app.use(express.json({limit: "50mb",  extended: true}));
app.use(express.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));

var port = process.env.PORT || 5000;
const uploadPath = path.join(__dirname, 'store/'); // Register the upload path
fs.ensureDir(uploadPath); // Make sure that he upload path exits

app.use(express.static("public"))

var storage = multer.diskStorage({
    // Multer Reference: https://www.youtube.com/watch?v=hXf8Rg-sdpA
    destination: function (req, file, cb) {
        cb(null, "store");
    },
    filename: function (req, file, cb) {
        cb(null, path.parse(file.originalname).name + "-" + Date.now() + path.extname(file.originalname));
    }
});

// Multer file size limit Reference: https://stackoverflow.com/a/34698643/6908282
var upload = multer({
    storage: storage,
    limits: { fileSize: '50mb' }
});
var multipleUploads = upload.fields([{ name: 'pdfToHighlight', maxCount: 1 }, { name: 'searchTerms', maxCount: 1 }])
app.post('/upload', multipleUploads, async (req, res) => {

})

app.listen(port, () => {
    if (port == 5000) {
        open("http://localhost:5000")
        console.log('server started at http://localhost:5000');
    }
})
    <form action="/upload" method="POST" enctype="multipart/form-data">
        <fieldset>
            <legend>File Uploads</legend>
            <label for="pdfFile">Select a PDF to Highlight:</label>
            <input type="file" accept="application/pdf" name="pdfToHighlight" id="pdfFile" required>
            <br>
            <label for="searchFile">Select a CSV with search terms:</label>
            <input type="file" accept=".csv" name="searchTerms" id="searchFile" required>
        </fieldset>
        <button style="background: lightseagreen;font-size: 1em;">upload</button>
    </form>

【问题讨论】:

    标签: node.js file-upload multer


    【解决方案1】:

    Basic Service Plan:

    基本服务计划专为流量要求较低且不需要高级自动缩放和流量管理功能的应用而设计。定价基于您运行的实例的大小和数量。内置网络负载平衡支持自动跨实例分配流量。 Linux 运行时环境的基本服务计划支持Web App for Containers

    您发布的错误是HTTP 413,这意味着数据太大而无法处理。 HTTP 413 Payload Too Large 响应状态码表示请求实体大于服务器定义的限制;

    您可能需要检查 azure 配置,如果有的话,您应该在其中更改配置。您还可以检查 this,标头配置。

    这可能是配置问题,因为它在本地运行良好,您还应该检查日志,如内存使用情况和 Web 应用托管的所有其他参数。

    【讨论】:

    • 感谢@Apoorva 的回复。原来这是 Azure Web 配置的问题
    【解决方案2】:

    经过大量研究并在同事的帮助下,我在 Azure 站点的 Kudu 仪表板(也称为 SCM 仪表板)中更新了 web.config 文件后,能够成功上传文件

    我添加了以下代码:

     <requestLimits maxAllowedContentLength="4294967295"  />
    

    【讨论】:

      猜你喜欢
      • 2020-02-04
      • 2021-08-16
      • 2019-09-04
      • 2017-04-20
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多