Skip to content

Instantly share code, notes, and snippets.

@Spittal
Created March 28, 2016 19:16
Show Gist options
  • Save Spittal/7b052cbbb9fea48bb86d to your computer and use it in GitHub Desktop.
Save Spittal/7b052cbbb9fea48bb86d to your computer and use it in GitHub Desktop.
public updateAvatar(file: File): Observable<any> {
return Observable.create((observer) => {
let formData: FormData = new FormData(),
xhr: XMLHttpRequest = new XMLHttpRequest();
formData.append('avatar', file, file.name);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
observer.next(JSON.parse(xhr.response));
observer.complete();
} else {
observer.error(JSON.parse(xhr.response));
observer.complete();
}
}
};
xhr.open('POST', ApiUrlHelper.apiV1Url + '/user/updateAvatar', true);
xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('id_token'));
xhr.send(formData);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment