How to download any kind of files with angularJs

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

2

Let’s see How to download any kind of files with angularJs

j1.png

 

j2.png

 

Note:-

Just we need to use contentType  in proper format

if (fileName.indexOf(“xls”) > -1 || fileName.indexOf(“xlsx”) > -1) {
var contentType = ‘application/octet-stream’;
} else if (fileName.indexOf(“pdf”) > -1) {
var contentType = ‘application/pdf’;
} else {
var contentType = ‘application/octet-stream’;
}
var fileDownLoadName = fileName;
var def = $q.defer();
$http.post(apiBase + “/portalDocumentDownload”, angular.toJson(helper), { responseType: ‘arraybuffer’ },
{ headers: { ‘Content-Type’: ‘application/json’ } })
.success(function (data, status, headers, config) {
var ieEDGE = navigator.userAgent.match(/Edge/g);
var ie = navigator.userAgent.match(/.NET/g); // IE 11+
var oldIE = navigator.userAgent.match(/MSIE/g);
var blob = new window.Blob([data], {
type: contentType
});
if (ie || oldIE || ieEDGE) {
window.navigator.msSaveBlob(blob, fileDownLoadName);
}
else {
var file = new Blob([data], { type: contentType });
var fileURL = URL.createObjectURL(file);
var a = document.createElement(‘a’);
a.href = fileURL;
a.target = ‘_blank’;
a.download = fileDownLoadName;
document.body.appendChild(a);
a.click();
}
})

Leave a comment