我想在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?
'fd.itemId = itemId'? – Dario
加了後端 – Serhiy
你可以直接將它附加到表單數據中...... –