Skip to content

Instantly share code, notes, and snippets.

@jesusmacedo
Last active February 9, 2016 19:13
Show Gist options
  • Save jesusmacedo/0903bd9a3b35931dfbb0 to your computer and use it in GitHub Desktop.
Save jesusmacedo/0903bd9a3b35931dfbb0 to your computer and use it in GitHub Desktop.
Download a received file converted into blob.
var type = format === 'XML'?'application/xml':'application/pdf';
var filename = format === 'XML'?'EstadodeCuenta.xml':'EstadodeCuenta.pdf';
$http({
url: $scope.servicesURL + 'getFile?format='+format,
method: "GET",
headers: {
'Content-type': 'application/json'
},
responseType: 'arraybuffer'
}).success(function (data, status, headers, config) {
var blob = new Blob([data], {type: type});
var objectUrl = URL.createObjectURL(blob);
// IE 10 || IE 11
if ( window.navigator.msSaveOrOpenBlob )
window.navigator.msSaveBlob(blob, filename);
// NOT IE browsers
else if ( 'download' in document.createElement('a') ) {
var a = document.createElement("a");
document.body.appendChild(a);
a.href = objectUrl;
a.download = filename;
a.click();
window.open(objectUrl);
// IE 9
} else
window.document.execCommand('SaveAs', null, objectUrl);
}).error(function (data, status, headers, config) {
// Handle error here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment