Skip to content

Instantly share code, notes, and snippets.

@msaxena25
Last active April 28, 2021 23:41
Show Gist options
  • Save msaxena25/dd698145f5917cc9211831328928051e to your computer and use it in GitHub Desktop.
Save msaxena25/dd698145f5917cc9211831328928051e to your computer and use it in GitHub Desktop.
Angular Open File in new window -- Get Byte stream from server and Open in Browser
getFile(id) {
this.newWindow = window.open();
this.downloadDocument(id).subscribe(response => { // API - this.downloadDocument(id)
this.util.removeLoader();
this.downloadFileFromBlob(response["_body"]);
}, error => { })
}
downloadFileFromBlob(data, newWindow = this.newWindow) {
//for chrome of ios
if (navigator.userAgent.match('CriOS')) {
let fileData = [data];
var reader = new FileReader();
var blob = new Blob(fileData, { type: 'application/pdf' });
reader.onload = function (e) {
window.location.href = reader.result;
}
reader.readAsDataURL(blob);
}
// for IE and edge browser
else if (window.navigator.msSaveOrOpenBlob) {
let fileData = [data];
let blobObject = new Blob(fileData);
window.navigator.msSaveBlob(blobObject);
}
//for all other browser
else {
let fileData = [data];
var blob = new Blob(fileData, { type: 'application/pdf' });
let url = window.URL.createObjectURL(blob);
newWindow.location = url; // open in new window
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment