[英]Upload File Angular 2
Currently playing around with Angular 2 RC. 目前正在玩Angular 2 RC。
Is there any tutorial/article that would help me understand uploading a file to the back-end via a REST call? 是否有任何教程/文章可以帮助我理解通过REST调用将文件上传到后端?
I've been through this , but it feels like there should be a more convenient way of doing it. 我经历过这个 ,但感觉应该有一个更方便的方法。
If you wait for RC2, you will have the ability to use other payloads than text ones. 如果您等待RC2,您将能够使用除文本之外的其他有效负载。
For example Blob
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
ones: 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
ones: 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 });
It would much easier to handle binary content for HTTP requests. 处理HTTP请求的二进制内容要容易得多。 Right now, it's difficult (and hacky) since Angular2 only accepts text payloads as input.
现在,由于Angular2仅接受文本有效负载作为输入,因此很难(并且很难)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.