Skip to content

Commit e482b34

Browse files
committed
update: move doesnload to file from wave
1 parent ab6f26c commit e482b34

File tree

3 files changed

+126
-20
lines changed

3 files changed

+126
-20
lines changed

dist/utils/downloader.d.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
export declare function downloadURL(url: string): void;
66
/**
7-
* Creates and downloads zip file.
7+
* Creates and downloads a zip file.
88
* @param zipName {String} Zip file name (without extension).
99
* @param files {Object[]} Job files to add to created zip.
1010
* @param onFileComplete {Function} Function to track progress.
@@ -18,11 +18,16 @@ type File = {
1818
signedUrl: string;
1919
};
2020
export declare function downloadZip(zipName: string, files: File[], onFileComplete: (file: File) => void, onError: (file: File, err: Error) => void, onComplete: () => void, prefix: string): void;
21+
export declare function exportToDisk(content: string | number | boolean, name?: string, extension?: string): void;
22+
export declare function saveStringDataToFile(strData: string, filename?: string): void;
23+
export declare function saveImageDataToFile(imgData: string, filename?: string): void;
2124
/**
22-
* Exports and downloads the content.
25+
* Exports and downloads the content to disk. Browser compatibility with IE.
2326
* @param content {String} Content to be saved in downloaded file
2427
* @param name {String} File name to be written on disk.
2528
* @param extension {String} File extension.
29+
* @param mime {String} type of the content.
30+
* Source: https://github.com/kennethjiang/js-file-download/blob/master/file-download.js
2631
*/
27-
export declare function exportToDisk(content: string | number | boolean, name?: string, extension?: string): void;
32+
export declare const exportToDiskLegacy: (content: string, name?: string, extension?: string, mime?: string) => void;
2833
export {};

dist/utils/downloader.js

+55-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import JSZip from "jszip";
22
// @ts-ignore
33
import * as JSZipUtils from "jszip-utils";
4-
import s from "underscore.string";
54
/**
65
* Downloads the specified storage file.
76
* @param url {String} URL to download.
@@ -36,15 +35,63 @@ export function downloadZip(zipName, files, onFileComplete, onError, onComplete,
3635
});
3736
});
3837
}
39-
/**
40-
* Exports and downloads the content.
41-
* @param content {String} Content to be saved in downloaded file
42-
* @param name {String} File name to be written on disk.
43-
* @param extension {String} File extension.
44-
*/
4538
export function exportToDisk(content, name = "file", extension = "txt") {
4639
const pom = document.createElement("a");
4740
pom.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(content));
48-
pom.setAttribute("download", s.sprintf(`%s.${extension}`, name));
41+
pom.setAttribute("download", `${name}.${extension}`);
4942
pom.click();
5043
}
44+
export function saveStringDataToFile(strData, filename = "data.txt") {
45+
const link = document.createElement("a");
46+
document.body.appendChild(link);
47+
link.download = filename;
48+
link.href = strData;
49+
link.click();
50+
document.body.removeChild(link);
51+
}
52+
export function saveImageDataToFile(imgData, filename = "screenshot.png") {
53+
try {
54+
saveStringDataToFile(imgData, `${filename}`);
55+
}
56+
catch (e) {
57+
console.error(e);
58+
}
59+
}
60+
/**
61+
* Exports and downloads the content to disk. Browser compatibility with IE.
62+
* @param content {String} Content to be saved in downloaded file
63+
* @param name {String} File name to be written on disk.
64+
* @param extension {String} File extension.
65+
* @param mime {String} type of the content.
66+
* Source: https://github.com/kennethjiang/js-file-download/blob/master/file-download.js
67+
*/
68+
export const exportToDiskLegacy = function exportToDisk(content, name = "file", extension = "txt", mime = "application/octet-stream") {
69+
const blob = new Blob([content], { type: mime });
70+
const filename = `${name}.${extension}`;
71+
// @ts-ignore
72+
if (typeof window.navigator.msSaveBlob !== "undefined") {
73+
// IE workaround for "HTML7007: One or more blob URLs were
74+
// revoked by closing the blob for which they were created.
75+
// These URLs will no longer resolve as the data backing
76+
// the URL has been freed."
77+
// @ts-ignore
78+
window.navigator.msSaveBlob(blob, filename);
79+
}
80+
else {
81+
const blobURL = window.URL.createObjectURL(blob);
82+
const tempLink = document.createElement("a");
83+
tempLink.style.display = "none";
84+
tempLink.href = blobURL;
85+
tempLink.setAttribute("download", filename);
86+
// Safari thinks _blank anchor are pop ups. We only want to set _blank
87+
// target if the browser does not support the HTML5 download attribute.
88+
// This allows you to download files in desktop safari if pop up blocking
89+
// is enabled.
90+
if (typeof tempLink.download === "undefined")
91+
tempLink.setAttribute("target", "_blank");
92+
document.body.appendChild(tempLink);
93+
tempLink.click();
94+
document.body.removeChild(tempLink);
95+
window.URL.revokeObjectURL(blobURL);
96+
}
97+
};

src/utils/downloader.ts

+63-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import JSZip from "jszip";
22
// @ts-ignore
33
import * as JSZipUtils from "jszip-utils";
4-
import s from "underscore.string";
54

65
/**
76
* Downloads the specified storage file.
@@ -17,7 +16,7 @@ export function downloadURL(url: string) {
1716
}
1817

1918
/**
20-
* Creates and downloads zip file.
19+
* Creates and downloads a zip file.
2120
* @param zipName {String} Zip file name (without extension).
2221
* @param files {Object[]} Job files to add to created zip.
2322
* @param onFileComplete {Function} Function to track progress.
@@ -58,15 +57,70 @@ export function downloadZip(
5857
});
5958
}
6059

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-
*/
6760
export function exportToDisk(content: string | number | boolean, name = "file", extension = "txt") {
6861
const pom = document.createElement("a");
6962
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}`);
7164
pom.click();
7265
}
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

Comments
 (0)