【发布时间】: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