1
1
import JSZip from "jszip" ;
2
2
// @ts -ignore
3
3
import * as JSZipUtils from "jszip-utils" ;
4
- import s from "underscore.string" ;
5
4
6
5
/**
7
6
* Downloads the specified storage file.
@@ -17,7 +16,7 @@ export function downloadURL(url: string) {
17
16
}
18
17
19
18
/**
20
- * Creates and downloads zip file.
19
+ * Creates and downloads a zip file.
21
20
* @param zipName {String} Zip file name (without extension).
22
21
* @param files {Object[]} Job files to add to created zip.
23
22
* @param onFileComplete {Function} Function to track progress.
@@ -58,15 +57,70 @@ export function downloadZip(
58
57
} ) ;
59
58
}
60
59
61
- /**
62
- * Exports and downloads the content.
63
- * @param content {String} Content to be saved in downloaded file
64
- * @param name {String} File name to be written on disk.
65
- * @param extension {String} File extension.
66
- */
67
60
export function exportToDisk ( content : string | number | boolean , name = "file" , extension = "txt" ) {
68
61
const pom = document . createElement ( "a" ) ;
69
62
pom . setAttribute ( "href" , "data:text/plain;charset=utf-8," + encodeURIComponent ( content ) ) ;
70
- pom . setAttribute ( "download" , s . sprintf ( `%s .${ extension } `, name ) ) ;
63
+ pom . setAttribute ( "download" , ` ${ name } .${ extension } `) ;
71
64
pom . click ( ) ;
72
65
}
66
+
67
+ export function saveStringDataToFile ( strData : string , filename = "data.txt" ) {
68
+ const link = document . createElement ( "a" ) ;
69
+ document . body . appendChild ( link ) ;
70
+ link . download = filename ;
71
+ link . href = strData ;
72
+ link . click ( ) ;
73
+ document . body . removeChild ( link ) ;
74
+ }
75
+
76
+ export function saveImageDataToFile ( imgData : string , filename = "screenshot.png" ) {
77
+ try {
78
+ saveStringDataToFile ( imgData , `${ filename } ` ) ;
79
+ } catch ( e ) {
80
+ console . error ( e ) ;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Exports and downloads the content to disk. Browser compatibility with IE.
86
+ * @param content {String} Content to be saved in downloaded file
87
+ * @param name {String} File name to be written on disk.
88
+ * @param extension {String} File extension.
89
+ * @param mime {String} type of the content.
90
+ * Source: https://github.com/kennethjiang/js-file-download/blob/master/file-download.js
91
+ */
92
+ export const exportToDiskLegacy = function exportToDisk (
93
+ content : string ,
94
+ name = "file" ,
95
+ extension = "txt" ,
96
+ mime = "application/octet-stream" ,
97
+ ) {
98
+ const blob = new Blob ( [ content ] , { type : mime } ) ;
99
+ const filename = `${ name } .${ extension } ` ;
100
+ // @ts -ignore
101
+ if ( typeof window . navigator . msSaveBlob !== "undefined" ) {
102
+ // IE workaround for "HTML7007: One or more blob URLs were
103
+ // revoked by closing the blob for which they were created.
104
+ // These URLs will no longer resolve as the data backing
105
+ // the URL has been freed."
106
+ // @ts -ignore
107
+ window . navigator . msSaveBlob ( blob , filename ) ;
108
+ } else {
109
+ const blobURL = window . URL . createObjectURL ( blob ) ;
110
+ const tempLink = document . createElement ( "a" ) ;
111
+ tempLink . style . display = "none" ;
112
+ tempLink . href = blobURL ;
113
+ tempLink . setAttribute ( "download" , filename ) ;
114
+
115
+ // Safari thinks _blank anchor are pop ups. We only want to set _blank
116
+ // target if the browser does not support the HTML5 download attribute.
117
+ // This allows you to download files in desktop safari if pop up blocking
118
+ // is enabled.
119
+ if ( typeof tempLink . download === "undefined" ) tempLink . setAttribute ( "target" , "_blank" ) ;
120
+
121
+ document . body . appendChild ( tempLink ) ;
122
+ tempLink . click ( ) ;
123
+ document . body . removeChild ( tempLink ) ;
124
+ window . URL . revokeObjectURL ( blobURL ) ;
125
+ }
126
+ } ;
0 commit comments