1

Currently playing around with Angular 2 RC.

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.

1 Answer 1

3

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

For example Blob ones:

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

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

Arraybuffer ones:

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:

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. Right now, it's difficult (and hacky) since Angular2 only accepts text payloads as input.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the info @Thierry, I've been looking for the status of those request methods, didn't manage to find it (;
@Sasxa you're welcome! RC2 was just released so you should find this in it ;-)
Yup, I saw release notes this morning (:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.