Angular post formdata和文件不在输入中

时间:2017-04-19 10:25:56

标签: angularjs

我使用Angular 1.5。

我必须调用需要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' ...); ?

1 个答案:

答案 0 :(得分: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
        });
    });