I implemented a method to POST a file in my Angular 2 app. It is based on the solution I found here.
Because Angular 2 doesn't support file uploads by itself, the solution has to leverage xhr
. This is what the working solution looks like:
The component method:
onSubmit(): void {
this.inputModuleService.postFile(this.files).subscribe(() => {
console.log('sent');
});
}
The service method:
postFile (files: File[]): Observable<string> {
var url = this.uploadURL;
return Observable.create(observer => {
var formData: FormData = new FormData()
var xhr: XMLHttpRequest = new XMLHttpRequest();
formData.append("upload", files[i], files[i].name);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
observer.next(JSON.parse(xhr.response));
observer.complete();
} else {
observer.error(xhr.response);
}
}
};
xhr.open('POST', url, true);
xhr.send(formData);
});
}
My issue is that I don't understand how to get the response back to the onSubmit()
method after the call to xhr.send()
. Angular2 and the concept of observables and promises are all new to me.
How can I get the server's response from this observable?