我想在http请求中发送itemId。如何做到这一点以及我将在服务器请求中看到什么?
这是请求:
var fd = new FormData();
var itemId = scope.vm.item._id;
fd.append('file', scope.files[0]);
$http.post('http://localhost:8090/file-upload', fd, {
transformRequest: angular.identity,
headers: {
'Content-Type': undefined
}
});
服务器
app.post('/file-upload', function(req, res, next) {
console.log("received file");
var pathFile = ' ';
var wId = "itemId";
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads/attachments');
pathFile = file.originalname;
},
filename: function (req, file,itemId, cb) {
cb(null, file.originalname);
console.log(itemId);
}
});
var upload = multer({ storage : storage }).single('file');
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
}
//save file path in work items doc.attachments
var path = './uploads/attachments/'+pathFile;
res.end("File is uploaded");
});
已添加服务代码。如何在服务器端获取itemId?
最佳答案
有几种方法可以做到这一点,您可以在帖子选项中设置 params 属性,并传入一个带有 id 的对象作为属性。
var fd = new FormData();
var itemId = scope.vm.item._id;
fd.append('file', scope.files[0]);
$http.post('http://localhost:8090/file-upload', fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined},
params: {id: itemId}
});
关于angularjs - 如何在请求中发送额外数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137636/