https://github.com/expressjs/multer
var express = require('express')
var multer = require('multer')
var app = express()
app.use(multer({ dest: './uploads/'}))
console.log(req.files)
看到上传文件只需要这样子配置,但是我想要判断大小商场的类型不符合就不上传,看了文档都不知道如何使用。如这个怎么使用?
onFileUploadData: function (file, data) {
console.log(data.length + ' of ' + file.fieldname + ' arrived')
}
我这样做有错误:
router.post('/up', function (req, res) {
if (req.file) {
onFileUploadData: function (file, data) {
console.log(data.length + ' of ' + file.fieldname + ' arrived')
}
}
});
请教一下。谢谢!
9 回复
@youqingkui 好像没有终止上传一说,如果接收的文件不是想要的,需要手动删之。。。看到这段
onFileSizeLimit: function (file) {
console.log('Failed: ', file.originalname)
fs.unlink('./' + file.path) // delete the partially written file
}
@youqingkui 额, 我这只是个理论,我没用过multer,我用的是connect-multiparty。 我看了下multer的使用方法好像是:
app.use(multer({
dest: './uploads/',
limits: {
fileSize: 100000000
}
}))
app.use(multer({
dest: './uploads/',
limits: {
fileSize: 100000000
},
onFileSizeLimit: function(file){
//如果大于100M,删除它
if(file.size > 100000000) {
fs.unlink('./' + file.path) // delete the partially written file
}
}
}))
@wengqianshan 那请问我不可以写在post提交后吗?
router.post('/up', function (req, res) {
if (req.file) {
onFileUploadData: function (file, data) {
console.log(data.length + ' of ' + file.fieldname + ' arrived')
}
}
});