Angular 2-将文件发布到Web API [英] Angular 2 - Post File to Web API

查看:77
本文介绍了Angular 2-将文件发布到Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Angular 2应用程序将文件传递到Web API,并且没有发送实际的文件数据.

I am attempting to pass a file to Web API from an Angular 2 app, and the actual file data does not get sent.

这是我的angular 2服务代码:

Here is my angular 2 service code:

        var headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');

        var formData = new FormData();
        formData.append("file", file);

        return this.http.post('http://myapiurl.com/files/upload', formData, { headers: headers }).map(res => res.json());

在angular 2文档中,POST方法签名如下:

In the angular 2 docs, the POST method signature is as follows:

post(url: string, body: string, options?: RequestOptionsArgs) : Observable<Response>

没有传递表格数据或其他对象的选项.只是正文请求中的一个字符串.当前的解决方法是使用javascript和常规的xhr请求.这样做有明显的弊端,例如无法使用可观察变量等.

There is no option to pass Form Data, or other object. Just a string in the bodys request. The current work around is to use javascript and regular xhr requests. There are obvious downsides to this like not being able to use observables, etc.

在此方面的任何帮助将不胜感激.

Any help on this would be greatly appreciated.

推荐答案

事实上,尚不支持此功能.请参阅此正在进行的问题: https://github.com/angular/angular/issues/5517 .

In fact, this isn't supported yet. See this in-progress issue: https://github.com/angular/angular/issues/5517.

HTTP支持的当前实现仅支持请求有效负载的文本内容.看到这两个文件

The current implementation of the HTTP support only supports text contents for request payload. See these two files

  • https://github.com/angular/angular/blob/master/modules/angular2/src/http/backends/xhr_backend.ts#L82
  • https://github.com/angular/angular/blob/master/modules/angular2/src/http/static_request.ts#L93

希望它对您有帮助, 蒂埃里

Hope it helps you, Thierry

这篇关于Angular 2-将文件发布到Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆