2017-02-09 7 views
1

httpリクエストでitemIdを送信します。それを行う方法と私はサーバーreqに何が表示されますか?追加データをリクエストするにはどうすればよいですか?

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"); 
}); 

コードが追加サーブ

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 
    } 
}); 

サーバー:ここ

が要求されます。どのようにサーバー側で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

が使用されましたが、何もparamsに入っていませんでした:しかし、querry.thanks !!!!!!!!!!!!!!!!!! – Serhiy

+0

は3分で回答を受け入れます – Serhiy

0

JSON文字列を以下のようにFromDataオブジェクトに追加できます。

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

サーバー側では、JSON文字列をparamsキーで取得します。

0

単純にパラメータをparamsに追加し、すべてのパラメータを渡します。

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

        }); 

これはあなたのためにうまくいきます。

関連する問題