我有一个问题,我尝试使用multer上传文件,但我得到了req.file undefined
。你能解释一下为什么会发生这种情况吗?
的index.html
<form enctype="multipart/form-data" name="sendProductForm" ng-submit="product.sendProduct(productData, sendProductForm.$valid)" class="col-md-8 col-md-offset-2 form-horizontal adding-form" novalidate>
<div class="form-group">
<div class="input-group">
<input type="text" name="image" class="form-control" ng-model="product.productData.image" readonly>
<span class="input-group-btn">
<input type="file" name="file" id="imgInp" onchange="angular.element(this).scope().photoChanged(this.files)">
</span>
</div>
</div>
</form>
api.js
router.post('/courses', multer({ dest:'./uploads/' }).single('file'), function(req, res){
console.log(req.body)
console.log(req.file)
})
serives
userFactory.createNewCourse = function(productData){
return $http.post('/api/courses', productData)
}
controller.js
app.sendProduct = function(productData, valid) {
if(valid){
User.createNewCourse(app.productData).then(function(data){
if(data.data.success){
app.successMsg = data.data.message + '...Redirecting';
} else {
app.disabled = false;
//app.loading = false;
app.errorMsg = data.data.message;
}
});
} else {
app.errorMsg = 'bad'
}
}
我检查了req.headers
,我得到了以下信息:
{ host: 'localhost:8080',
connection: 'keep-alive',
'content-length': '156',
origin: 'http://localhost:8080',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
'content-type': 'application/json;charset=UTF-8',
accept: 'application/json, text/plain, */*',
'x-access-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkR1cGFhYSIsImVtYWlsIjoidHV0b3JyaWFsbWVhbkBnbWFpbC5jb20iLCJpYXQiOjE1MDE1MzA2NzAsImV4cCI6MTUwMTYxNzA3MH0.r_16sP6Jbeddt6rGeeRnSPkO66XqkaQYFtJhP829YFQ',
referer: 'http://localhost:8080/newCourse',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4',
cookie: 'connect.sid=s%3ADzmgNuLHUVcE-hnNJKPfUZUyxLXYpSXC.TiF5B7aBjiIPJtyqVeaSzd7WMcYH16CGY53SRWXR1w8' }
这是正确的标题吗?