hsu-utils 0.0.35 → 0.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hsu-utils.js +3 -3
- package/dist/hsu-utils.min.js +1 -1
- package/dist/hsu-utils.min.js.LICENSE.txt +1 -1
- package/es/DownloadFile/index.d.ts +1 -1
- package/es/DownloadFile/index.js +24 -23
- package/lib/DownloadFile/index.d.ts +1 -1
- package/lib/DownloadFile/index.js +64 -24
- package/package.json +1 -1
@@ -1 +1 @@
|
|
1
|
-
export default function downloadFile(file: ArrayBuffer | string, fileName?: string): void;
|
1
|
+
export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string): void;
|
package/es/DownloadFile/index.js
CHANGED
@@ -1,27 +1,28 @@
|
|
1
|
-
function
|
2
|
-
|
3
|
-
|
4
|
-
.
|
5
|
-
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
6
8
|
});
|
7
|
-
}
|
9
|
+
};
|
10
|
+
import { Typeof } from '..';
|
8
11
|
function downloadFileByUrl(url, fileName) {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
downloadElement.click();
|
24
|
-
document.body.removeChild(downloadElement);
|
12
|
+
return __awaiter(this, void 0, void 0, function* () {
|
13
|
+
try {
|
14
|
+
const response = yield fetch(url);
|
15
|
+
const arrayBuffer = yield response.arrayBuffer();
|
16
|
+
downloadFile(arrayBuffer, fileName);
|
17
|
+
}
|
18
|
+
catch (_a) {
|
19
|
+
const downloadElement = document.createElement('a');
|
20
|
+
downloadElement.href = url;
|
21
|
+
downloadElement.download = decodeURIComponent(fileName || '');
|
22
|
+
document.body.appendChild(downloadElement);
|
23
|
+
downloadElement.click();
|
24
|
+
document.body.removeChild(downloadElement);
|
25
|
+
}
|
25
26
|
});
|
26
27
|
}
|
27
28
|
export default function downloadFile(file, fileName) {
|
@@ -29,7 +30,7 @@ export default function downloadFile(file, fileName) {
|
|
29
30
|
downloadFileByUrl(file, fileName);
|
30
31
|
return;
|
31
32
|
}
|
32
|
-
const blob = new Blob([file]);
|
33
|
+
const blob = Typeof(file) === 'blob' ? file : new Blob([file]);
|
33
34
|
const downloadElement = document.createElement('a');
|
34
35
|
const href = window.URL.createObjectURL(blob);
|
35
36
|
downloadElement.href = href;
|
@@ -1 +1 @@
|
|
1
|
-
export default function downloadFile(file: ArrayBuffer | string, fileName?: string): void;
|
1
|
+
export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string): void;
|
@@ -1,29 +1,69 @@
|
|
1
1
|
"use strict";
|
2
|
-
|
3
|
-
function
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
9
|
});
|
9
|
-
}
|
10
|
-
function
|
11
|
-
if (
|
12
|
-
|
13
|
-
|
10
|
+
};
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
14
36
|
}
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
var __1 = require("..");
|
40
|
+
function downloadFileByUrl(url, fileName) {
|
41
|
+
return __awaiter(this, void 0, void 0, function () {
|
42
|
+
var response, arrayBuffer, _a, downloadElement;
|
43
|
+
return __generator(this, function (_b) {
|
44
|
+
switch (_b.label) {
|
45
|
+
case 0:
|
46
|
+
_b.trys.push([0, 3, , 4]);
|
47
|
+
return [4, fetch(url)];
|
48
|
+
case 1:
|
49
|
+
response = _b.sent();
|
50
|
+
return [4, response.arrayBuffer()];
|
51
|
+
case 2:
|
52
|
+
arrayBuffer = _b.sent();
|
53
|
+
downloadFile(arrayBuffer, fileName);
|
54
|
+
return [3, 4];
|
55
|
+
case 3:
|
56
|
+
_a = _b.sent();
|
57
|
+
downloadElement = document.createElement('a');
|
58
|
+
downloadElement.href = url;
|
59
|
+
downloadElement.download = decodeURIComponent(fileName || '');
|
60
|
+
document.body.appendChild(downloadElement);
|
61
|
+
downloadElement.click();
|
62
|
+
document.body.removeChild(downloadElement);
|
63
|
+
return [3, 4];
|
64
|
+
case 4: return [2];
|
65
|
+
}
|
66
|
+
});
|
27
67
|
});
|
28
68
|
}
|
29
69
|
function downloadFile(file, fileName) {
|
@@ -31,7 +71,7 @@ function downloadFile(file, fileName) {
|
|
31
71
|
downloadFileByUrl(file, fileName);
|
32
72
|
return;
|
33
73
|
}
|
34
|
-
var blob = new Blob([file]);
|
74
|
+
var blob = (0, __1.Typeof)(file) === 'blob' ? file : new Blob([file]);
|
35
75
|
var downloadElement = document.createElement('a');
|
36
76
|
var href = window.URL.createObjectURL(blob);
|
37
77
|
downloadElement.href = href;
|