Skip to main content

File Download Example

Note

This code is missing the commonServices.getFileObject code referenced in line 3.

function link($scope, $elem) {
function download() {
commonServices.getFileObject($scope.fileId, $scope.moduleId).then(result => {
let blob = new Blob([result.data]),
disposition = result.headers['content-disposition'].replace('attachment; ', ''),
filename = disposition.split('=')[1];


if (window.navigator && window.navigator.msSaveOrOpenBlob) {
//Open file IE
window.navigator.msSaveOrOpenBlob(blob, filename);
} else {
//Open file browser other than IE
let a = document.createElement('a');


a.style.display = 'none';
document.body.appendChild(a);
a.href = window.URL.createObjectURL(blob);
a.download = filename;


window.setTimeout(() => {
a.click();
document.body.removeChild(a);
}, 500);
}
});
}


$elem.on('click', download);


$scope.$on('$destroy', function () {
$elem.off('click');
});
}