Multer |停止文件上传 [英] Multer | Stop file upload
本文介绍了Multer |停止文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用expressjs 4.0和multer来处理文件上传。
I am using expressjs 4.0 along with multer to handle file uploads.
问题是,当文件的大小超过maxSize变量时,我想停止文件上传。但是
multer无论如何上传文件。
Problem is, I want to stop file upload when the file's size exceeds a maxSize variable. But multer uploads file anyways.
有没有办法来阻止这种行为。
Is there a way to stop this behaviour.
示例代码:
app.post('/upload', function(req, res) {
var maxSize = 32 * 1000 * 1000 // 32mb max
if(req.files.file.length > maxSize) {
// stop upload
} else {
// continue
res.send(201);
}
});
推荐答案
根据Multer文档:( https://www.npmjs.org/package/multer ),您可以使用onFileUploadStart(file),并返回false if大小超过你的极限。
According to Multer documentation: (https://www.npmjs.org/package/multer) you could use onFileUploadStart(file), and return false if the size is above your limit.
例如:
var express = require('express');
var multer = require('multer');
var maxSize = 32 * 1000 * 1000;
var app = express();
app.use(multer({
dest: './uploads/',
onFileUploadStart: function(file, req, res){
if(req.files.file.length > maxSize) {
return false;
}
})
}));
这篇关于Multer |停止文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文