我正在使用上面提到的fileuplaod中间件。我想设置一个中间件,警告文件大小超过限制,但不想截断文件。
app.use(fileUpload({
limits: { fileSize: 1 * 1024 * 1024 },
createParentPath: true,
safeFileNames: true,
preserveExtension: 5,
limitHandler: function (req, res, next) {
Logger.warn("File size limit has been exceeded");
},
abortOnLimit: false,
useTempFiles: true,
tempFileDir: './temp/apiuploads'
}));
文件正在被截断。因此,上传的文件无法打开。我想在超过文件大小限制时记录消息,但不想截断文件。请给我一个解决方案。
发布于 2020-04-08 19:53:09
不要为中间件添加文件限制选项,我也建议只对上传路由使用中间件。
使用size值检查上传文件后的大小。
如果大小超过您的限制,则记录消息。
示例:
app.use('/upload', fileUpload({
createParentPath: true,
safeFileNames: true,
preserveExtension: 5,
useTempFiles: true,
tempFileDir: './temp/apiuploads'
}));
app.post('/upload', (req, res) => {
if (!req.files || Object.keys(req.files).length === 0) {
res.status(400).send('No files were uploaded.');
return;
}
const uploads = Object.values(req.files).map((file) => {
if (file.size > 1 * 1024 * 1024) {
Logger.warn(`File ${file.name} size limit has been exceeded`);
}
const uploadPath = path.join(__dirname, 'uploads', file.name);
return file.mv(uploadPath);
});
Promise.all(uploads)
.then(() => res.send('Files uploaded to ' + path.join(__dirname, 'uploads')))
.catch(err => res.status(500).send(err));
});
https://stackoverflow.com/questions/59014701
复制相似问题