2017-02-09 60 views
1

我想在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?

+1

'fd.itemId = itemId'? – Dario

+0

加了後端 – Serhiy

+0

你可以直接將它附加到表單數據中...... –

回答

1

這是一對夫婦的一種方式,你可以做到這一點,你可以在你的發佈選項中設置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} 

       }); 
+0

已使用但沒有任何參數:但在querry.thanks !!!!!!!!!!!!!!!!!! – Serhiy

+0

將在3分鐘內接受答案 – Serhiy

0

您可以將JSON字符串附加到FromData對象,如下所示。

fd.append('params',JSON.stringify({itemId:itemId}));

在服務器端,您將在params鍵中獲得JSON字符串。

0

簡單地在params中添加你的參數並傳遞你所有的參數。

$http.post('http://localhost:8090/file-upload', fd, { 
       transformRequest: angular.identity, 
         headers: {'Content-Type': undefined}, 
         params: {id: scope.vm.item._id} 

        }); 

希望這對你有用。

相關問題