2017-04-19 29 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 
     }); 
    });