Javascript 使用multer上载多个文件?
我就是不明白为什么我的代码在下面。我尝试上传多个文件。 问题是我有一个“意外字段” 我可以上传一个文件,但我不能上传多个文件。那么这里怎么了 html controllerArticle.jsJavascript 使用multer上载多个文件?,javascript,angularjs,node.js,express,multer,Javascript,Angularjs,Node.js,Express,Multer,我就是不明白为什么我的代码在下面。我尝试上传多个文件。 问题是我有一个“意外字段” 我可以上传一个文件,但我不能上传多个文件。那么这里怎么了 html controllerArticle.js function postArticles(req, res) { // console.log(req.file) if (req.file) { const nvo = new articulos({ name: req.body.name, price: req.body.
function postArticles(req, res) {
// console.log(req.file)
if (req.file) {
const nvo = new articulos({
name: req.body.name,
price: req.body.price,
condition: req.body.condition,
brand: req.body.brand,
productImage: req.file.path
})
nvo.save((err, productStored) => {
if (err) res.status(500)
res.status(200).send({ nvo: productStored })
})
}
}
到目前为止,我已经尝试过:
将upload.array()更改为upload.any(),但不起作用。。
有什么帮助吗?谢谢 已经解决了。。。问题出在控制器上。 Is req.files不是req.file(单数)。 如果我只需要上传一个文件,它是req.file,多个文件是req.files
var payload = new FormData();
var files = $('#file')[0].files
for (key in $scope.producto) {
payload.append(key, $scope.producto[key])
console.log($scope.producto[key])
}
if (files.length > 0) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.log(file)
console.log(files.length)
payload.append('image', file);
}
}
$http.post('http://localhost:5001/api/v1/articles', payload, {
transformRequest: angular.identity,
headers: { "Content-Type": undefined },
}).then(function (response) {
$location.path("/")
})
const multer = require('multer')
const storage = multer.diskStorage({
destination : function(req, file, cb){
cb(null, './uploads/');
},
filename : function(req, file, cb){
cb(null, new Date().toISOString() + file.originalname)
}
})
const upload = multer({storage : storage, limits: {
fileSize : 1024 * 1024 * 5
}})
.post('/api/v1/articles/', upload.array('image', 2 ), controllerArticle.postArticles)
function postArticles(req, res) {
// console.log(req.file)
if (req.file) {
const nvo = new articulos({
name: req.body.name,
price: req.body.price,
condition: req.body.condition,
brand: req.body.brand,
productImage: req.file.path
})
nvo.save((err, productStored) => {
if (err) res.status(500)
res.status(200).send({ nvo: productStored })
})
}
}