Skip to content

Instantly share code, notes, and snippets.

@chen0040
Created March 18, 2018 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chen0040/2bacc0bf16a551940eaae7df8d3828c0 to your computer and use it in GitHub Desktop.
Save chen0040/2bacc0bf16a551940eaae7df8d3828c0 to your computer and use it in GitHub Desktop.
getBinaryObjById(id: number, token: string) {
const req = {
token: string
};
Observable.create(observer => {
let xhr = new XMLHttpRequest();
xhr.open('POST', './erp/binary-obj/find-binary-obj-by-id?id=' + id, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.responseType='blob';
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var contentType = "application/octet-stream";
var blob = new Blob([xhr.response], { type: contentType });
console.log(blob);
observer.next(blob);
observer.complete();
} else {
observer.error(xhr.response);
}
}
};
xhr.send(JSON.stringify(req));
}).subscribe(blob => {
console.log(blob);
let reader = new FileReader();
reader.onloadend = function () {
window.location.href = reader.result;
};
reader.readAsDataURL(blob);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment