Last active
February 9, 2016 19:13
-
-
Save jesusmacedo/0903bd9a3b35931dfbb0 to your computer and use it in GitHub Desktop.
Download a received file converted into blob.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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