如何将uploadYear
从Angular服务传递到Multer目的地,以获得动态路径,例如' ./ public / uploads /' + uploadedYear?
闷闷不乐的工作方式,对我来说似乎非常扭曲,而且我无法确定在哪里存放一个额外的物体,所以它可以一直到达Multer目的地。
基本上我看不到myfile
如何从前端到后端的路径,或者我如何在后端访问它。
角色服务
this.uploadCV = function (file, uploadYear) {
console.log(uploadYear) //output 2017
var fd = new FormData()
fd.append('myfile', file.upload)
return $http.post('/api/uploadCV', fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
})
}
API
var cv = ''
var CVstorage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/uploads')
},
filename: function (req, file, cb) {
if (!file.originalname.match(/\.(jpg)$/)) {
var err = new Error()
err.code = 'filetype'
return cb(err)
} else {
cv = file.originalname
cb(null, cv)
}
}
})
var uploadCV = multer(
{
storage: CVstorage,
limits: { fileSize: 10000000 } //limit 10 MB
})
.single('myfile')
//API Route
router.post('/uploadCV', function (req, res) {
uploadCV(req, res, function (err) {
if (!req.file) {
//error handle
} else {
res.json({ success: true, cv: cv })
}
})
})
答案 0 :(得分:2)
您可以在标题中将其作为参数发送:
$http.post('/api/uploadCV', fd, {
transformRequest: angular.identity,
headers: {
'Content-Type': undefined,
'path': uploadYear
}
});
然后在你的后端你可以通过 req.headers.path
获得它