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')
}
}
});
请教一下。谢谢!
理论上讲,可以通过app.use(multer({ dest: './uploads/', maxSize: xxxxx}))
来设置
@wengqianshan 那我如何在post提交后进行处理呢?如图片类型不是我想要的,我要终止上传。
@youqingkui 好像没有终止上传一说,如果接收的文件不是想要的,需要手动删之。。。看到这段
onFileSizeLimit: function (file) {
console.log('Failed: ', file.originalname)
fs.unlink('./' + file.path) // delete the partially written file
}
@wengqianshan 请问这个也要放在这里吗?app.use(multer({ dest: ‘./uploads/’, maxSize: xxxxx})) 谢谢!
@youqingkui 额, 我这只是个理论,我没用过multer,我用的是connect-multiparty。 我看了下multer的使用方法好像是:
app.use(multer({
dest: './uploads/',
limits: {
fileSize: 100000000
}
}))
@wengqianshan 请问我该如何使用onFileSizeLimit, 这好像是个监听。谢谢!
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')
}
}
});
你这个文件能上传上么 我这个设置dest上传完之后,路径下没有我上传的文件
https://github.com/jpfluger/multer/blob/examples/multer-upload-files-to-different-directories.md
<pre>
var mwMulter1 = multer({ dest: ‘./uploads1/’ });
app.post(’/files1’, mwMulter1, function(req, res) {
console.log('IN POST (/files1)');
console.log(req.body)
var filesUploaded = 0;
if ( Object.keys(req.files).length === 0 ) {
console.log('no files uploaded');
} else {
console.log(req.files)
var files = req.files.file1;
if (!util.isArray(req.files.file1)) {
files = [ req.files.file1 ];
}
filesUploaded = files.length;
}
res.json({ message: 'Finished! Uploaded ' + filesUploaded + ' files. Route is /files1' });
});
</pre>
正式上线了之后也是Express暴露给浏览器?如果前面还有HTTP Server,类似Nginx之类,可以通过它们的POST BODY LIMIT来限制大文件的上传,Express里面就不用再写多余代码了。不知道这样满足不满足你的要求。
服务器收到上传文件后,怎样提醒前端页面上传成功呢?因为不是ajsx提交的所以我没有办法像ajax的success中收到服务器发来的json数据,这该怎么办呢?