上传文件Angular 2 [英] Upload File Angular 2

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

问题描述

当前正在使用Angular 2 RC.

Currently playing around with Angular 2 RC.

是否有任何教程/文章可以帮助我理解通过REST调用将文件上传到后端?

Is there any tutorial/article that would help me understand uploading a file to the back-end via a REST call?

我已经通过,但是感觉应该更方便的方式.

I've been through this, but it feels like there should be a more convenient way of doing it.

推荐答案

如果您等待RC2,则可以使用文本以外的其他有效载荷.

If you wait for RC2, you will have the ability to use other payloads than text ones.

例如Blob个:

var headers = new Headers({'Content-Type': 'text/css'});
var body = new Blob(['body { color: red; }']);

return this.http.post('/url', body, { headers });

Arraybuffer个:

var headers = new Headers({'Content-Type': 'text/css'});

var body = new ArrayBuffer(512);
var longInt8View = new Uint8Array(body);
for (var i = 0; i < longInt8View.length; i++) {
  longInt8View[i] = i % 255;
}

return this.http.post('/url', body, { headers });

FormData个:

var body = new FormData();
body.append('test1', 'val1');
body.append('test2', 123456);
var blob = new Blob(['body { color: red; }'], {type: 'text/css'});
body.append("userfile", blob);

return this.http.post('/url', body, { headers });

处理HTTP请求的二进制内容会容易得多.现在,这很困难(而且很棘手),因为Angular2仅接受文本有效载荷作为输入.

It would much easier to handle binary content for HTTP requests. Right now, it's difficult (and hacky) since Angular2 only accepts text payloads as input.

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

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