|
|
@@ -0,0 +1,84 @@
|
|
|
+type NavigatorWithSaveBlob = Navigator & {
|
|
|
+ msSaveBlob?: (blob: Blob, defaultName?: string) => boolean;
|
|
|
+};
|
|
|
+
|
|
|
+function saveBlobForLegacyBrowser(blob: Blob, fileName: string) {
|
|
|
+ const nav = navigator as NavigatorWithSaveBlob;
|
|
|
+ if (typeof nav.msSaveBlob === "function") {
|
|
|
+ nav.msSaveBlob(blob, fileName);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function downloadCallback(res: { data: any; }, fileName: string) {
|
|
|
+ const content = res.data;
|
|
|
+ const blob = new Blob([content]);
|
|
|
+ if ("download" in document.createElement("a")) {
|
|
|
+ // 非IE下载
|
|
|
+ const elink = document.createElement("a");
|
|
|
+ elink.download = fileName;
|
|
|
+ elink.style.display = "none";
|
|
|
+ elink.href = URL.createObjectURL(blob);
|
|
|
+ document.body.appendChild(elink);
|
|
|
+ elink.click();
|
|
|
+ URL.revokeObjectURL(elink.href); // 释放URL 对象
|
|
|
+ document.body.removeChild(elink);
|
|
|
+ } else {
|
|
|
+ // IE10+下载
|
|
|
+ saveBlobForLegacyBrowser(blob, fileName);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function downloadCallback2(res: { data: any; }, fileName: string) {
|
|
|
+ const content = res.data;
|
|
|
+ let blob: Blob;
|
|
|
+
|
|
|
+ if (content instanceof Blob) {
|
|
|
+ blob = content;
|
|
|
+ } else if (fileName.toLowerCase().endsWith(".csv")) {
|
|
|
+ let csvContent: string;
|
|
|
+
|
|
|
+ if (typeof content === "string") {
|
|
|
+ csvContent = content;
|
|
|
+ } else if (content instanceof ArrayBuffer) {
|
|
|
+ csvContent = typeof TextDecoder !== "undefined" ? new TextDecoder("utf-8").decode(content) : String(content);
|
|
|
+ } else if (ArrayBuffer.isView(content)) {
|
|
|
+ const view = content as ArrayBufferView;
|
|
|
+ csvContent = typeof TextDecoder !== "undefined" ? new TextDecoder("utf-8").decode(view) : String(content);
|
|
|
+ } else {
|
|
|
+ csvContent = String(content);
|
|
|
+ }
|
|
|
+
|
|
|
+ blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
|
|
|
+ } else if (fileName.toLowerCase().endsWith(".zip")) {
|
|
|
+ const zipParts: BlobPart[] = [];
|
|
|
+
|
|
|
+ if (content instanceof ArrayBuffer || ArrayBuffer.isView(content)) {
|
|
|
+ zipParts.push(content);
|
|
|
+ } else if (typeof content === "string") {
|
|
|
+ if (typeof TextEncoder !== "undefined") {
|
|
|
+ zipParts.push(new TextEncoder().encode(content));
|
|
|
+ } else {
|
|
|
+ zipParts.push(content);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ zipParts.push(content as BlobPart);
|
|
|
+ }
|
|
|
+
|
|
|
+ blob = new Blob(zipParts, { type: "application/zip" });
|
|
|
+ } else {
|
|
|
+ blob = new Blob([content]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("download" in document.createElement("a")) {
|
|
|
+ const elink = document.createElement("a");
|
|
|
+ elink.download = fileName;
|
|
|
+ elink.style.display = "none";
|
|
|
+ elink.href = URL.createObjectURL(blob);
|
|
|
+ document.body.appendChild(elink);
|
|
|
+ elink.click();
|
|
|
+ URL.revokeObjectURL(elink.href);
|
|
|
+ document.body.removeChild(elink);
|
|
|
+ } else {
|
|
|
+ saveBlobForLegacyBrowser(blob, fileName);
|
|
|
+ }
|
|
|
+}
|