Skip to content

Commit 07d742f

Browse files
author
Philipp Alferov
committed
Add support for blob object as parameter
1 parent d392b21 commit 07d742f

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/file-saver.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/* angular-file-saver
22
*
3-
* Provides integration between FileSaver.js (implements HTML5 saveAs())
4-
* and angular
3+
* Provides integration between FileSaver.js and Angular
54
*
6-
* (c) 2015 Philipp Alferiov
5+
* (c) 2015 Philipp Alferov
76
* License: MIT
87
*
98
*/
@@ -16,10 +15,10 @@
1615
.factory('SaveAs', [SaveAs]);
1716

1817
function SaveAs () {
19-
function handleBlob(data, type) {
18+
function blobInit(data, type) {
2019
var blob;
2120

22-
if (typeof(Blob) === "function") {
21+
if (hasBlobSupport()) {
2322
return new Blob(data, type);
2423
}
2524

@@ -30,14 +29,37 @@
3029
return blob;
3130
}
3231

32+
function isBlobInstance(data) {
33+
var blobSupport = hasBlobSupport();
34+
35+
if (blobSupport && data instanceof Blob) {
36+
return true;
37+
}
38+
39+
if (!blobSupport && data instanceof BlobBuilder) {
40+
return true;
41+
}
42+
43+
return false;
44+
}
45+
46+
function hasBlobSupport () {
47+
return typeof(Blob) === "function";
48+
}
49+
3350
return {
34-
saveFile: function (data, type, filename) {
35-
if (data instanceof Blob) {
36-
//TODO: implement Blob instance support
51+
saveFile: function (filename, settings) {
52+
var data = settings.data;
53+
var type = settings.options;
54+
55+
56+
if (isBlobInstance(data)) {
57+
return saveAs(data, filename);
3758
}
38-
var blob = handleBlob(data, type);
3959

40-
saveAs(blob, filename);
60+
var blob = blobInit(data, type);
61+
62+
return saveAs(blob, filename);
4163
}
4264

4365
};

0 commit comments

Comments
 (0)