2017-04-19 34 views
0

我使用Angular 1.5。角度帖子formdata和文件不在輸入

我要叫這就需要FORMDATA服務:

var fd = new FormData(); 
    fd.append('files', []); 
    fd.append(name, data); 

    return $http.post(API + uri, fd, { 
      transformRequest: angular.identity, 
      headers: {'Content-Type': undefined} 
     }) 

我的問題是我必須參加我已通過API上傳的文件。該文件不被用戶上傳。

如何聲明文件變量?

var file = $http.get('api/getFile' ...); ? 

回答

0

你的問題有點含糊,但我認爲你想要這樣的東西?

您需要從then回調中的GET請求獲得響應,然後在該回調中發送POST請求。 $ http.get和$ http.post的返回值是promises,而不是響應。

$http.get("api/getFile") 
    .then(function(response) { 
     var file = response.plain(); 
     // do stuff with file and fd .. 
     $http.post(API + uri, fd, { 
      transformRequest: angular.identity, 
      headers: {'Content-Type': undefined} 
     }) 
     .then(function(response){ 
      // do things with response from POST 
     }); 
    });