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();
}
})

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s