-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-download-service.js
executable file
·125 lines (110 loc) · 3.8 KB
/
angular-download-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(function () {
'use strict';
var app = angular.module('angular.download.service', []);
function FileDownload() {
this.strMimeType = 'application/octet-stream;charset=utf-8';
/**
* @ngdoc function
* @name setMimeType
* @methodOf angular.download.service:downloadService
* @param {string} mimeType Mime type for the download
* @description Sets mime type for current download
*
* Original function from: https://github.com/angular-ui/ng-grid/blob/master/src/features/exporter/js/exporter.js
*/
this.setMimeType = function (mimeType) {
this.strMimeType = mimeType;
}
/**
* @ngdoc function
* @name isIE
* @methodOf angular.download.service:downloadService
* @description Checks whether current browser is IE and returns it's version if it is
*
* Original function from: https://github.com/angular-ui/ng-grid/blob/master/src/features/exporter/js/exporter.js
*/
this.isIE = function () {
var match = navigator.userAgent.match(/(?:MSIE |Trident\/.*; rv:)(\d+)/);
return match ? parseInt(match[1]) : false;
}
/**
* @ngdoc function
* @name isSafari
* @methodOf angular.download.service:downloadService
* @description Checks whether current browser is Safari and returns it's version if it is
*/
this.isSafari = function () {
var match = navigator.userAgent.match('(Version)/(\\d+)\\.(\\d+)(?:\\.(\\d+))?.*Safari/');
return match ? parseInt(match[2]) : false;
}
/**
* @ngdoc function
* @name downloadFile
* @methodOf angular.download.service:downloadService
* @param {string} fileName Name of the file that will be downloaded
* @param {string} content Base64 encoded file content
* @description Launch a file download witch specified filename and data
*
* Original function from: https://github.com/angular-ui/ng-grid/blob/master/src/features/exporter/js/exporter.js
*/
this.downloadFile = function (fileName, content) {
var D = document;
var a = D.createElement('a');
var rawFile;
var ieVersion;
ieVersion = this.isIE();
if (ieVersion && ieVersion < 10) {
var frame = D.createElement('iframe');
document.body.appendChild(frame);
frame.contentWindow.document.open("text/html", "replace");
frame.contentWindow.document.write('sep=,\r\n' + content);
frame.contentWindow.document.close();
frame.contentWindow.focus();
frame.contentWindow.document.execCommand('SaveAs', true, fileName);
document.body.removeChild(frame);
return true;
}
// IE10+
if (navigator.msSaveBlob) {
return navigator.msSaveBlob(
new Blob([content], {
type: this.strMimeType
}),
fileName
);
}
//html5 A[download]
if ('download' in a) {
var blob = new Blob(
[content], {
type: this.strMimeType
}
);
rawFile = URL.createObjectURL(blob);
a.setAttribute('download', fileName);
} else {
rawFile = 'data:' + this.strMimeType + ',' + encodeURIComponent(content);
a.setAttribute('target', '_blank');
}
a.href = rawFile;
a.setAttribute('style', 'display:none;');
D.body.appendChild(a);
setTimeout(function () {
if (a.click) {
a.click();
// Workaround for Safari 5
} else if (document.createEvent) {
var eventObj = document.createEvent('MouseEvents');
eventObj.initEvent('click', true, true);
a.dispatchEvent(eventObj);
}
D.body.removeChild(a);
}, this.delay);
}
}
app.factory('fileDownloadService',
function () {
return new FileDownload();
}
);
})();