emacroh5lib 1.0.19 → 1.0.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emacroh5lib",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "EMacro前端组件库",
5
5
  "main": "dist/emacroh5lib.min.js",
6
6
  "scripts": {
@@ -1,3 +1,4 @@
1
+
1
2
  import * as XLSX from "xlsx";
2
3
 
3
4
  export namespace EMacro {
@@ -6,7 +7,76 @@ export namespace EMacro {
6
7
 
7
8
  export class File {
8
9
 
9
- private static selectFile(options: any = { multiple: true, accept: "*/*" }) {
10
+ public static getFileMD5(file: File): string {
11
+
12
+ throw "NG"
13
+ }
14
+
15
+ private static getBase64(blob) {
16
+ return new Promise((resolve, reject) => {
17
+ const reader = new FileReader();
18
+ reader.readAsDataURL(blob);
19
+ reader.onload = () => resolve(reader.result);
20
+ reader.onerror = error => reject(error);
21
+ });
22
+ }
23
+
24
+ public static getFileChunks(file: any): any {
25
+ let { size } = file
26
+ const sliceSize = 1024 * 1024 * 2;
27
+ let totalChunks = Math.ceil(size / sliceSize)
28
+ let fileChunks = new Array<any>()
29
+ if (size > sliceSize) {
30
+ for (let i = 0; i < totalChunks; i++) {
31
+ let start = i * sliceSize
32
+ let end = (i + 1) * sliceSize
33
+ let chunk = file.slice(start, end)
34
+ fileChunks.push(chunk)
35
+ }
36
+ } else {
37
+ fileChunks.push(file)
38
+ }
39
+ return { "fileChunks": fileChunks, "sliceSize": sliceSize, "chunksLength": fileChunks.length }
40
+ }
41
+
42
+ // 文件上传
43
+ public static uploadFiles(files, savePath, postFunc, errFunc, uploadedFunc) {
44
+
45
+ if (files[0].fileName == "" || files[0].fileSize == "" || files[0].fileSize == 0) {
46
+ throw new Error("请选择要上传的文件")
47
+ }
48
+
49
+ let that = this
50
+ for (let i = 0; i < files.length; i++) {
51
+ const file = files[i];
52
+ let { fileChunks, sliceSize, chunksLength } = File.getFileChunks(file.file)
53
+ for (let index in fileChunks) {
54
+ let chunk = fileChunks[Number(index)]
55
+
56
+ setTimeout(async () => {
57
+ await File.getBase64(chunk).then((base64: any) => {
58
+ let data = {
59
+ 'chunkIndex': Number(index), // 分片位置
60
+ 'chunksLength': chunksLength, // 分片长度
61
+ 'sliceSize': sliceSize, // 分片大小
62
+ 'chunkSize': chunk.size, // 当前分片实际大小
63
+ 'fileSize': file.file.size, // 文件总大小
64
+ 'fileName': file.file.name, // 文件名
65
+ 'data': base64.split(';base64,')[1], // 文件数据
66
+ 'lastModifiedDate': file.file.lastModifiedDate, // 最后修改时间
67
+ 'dataType': 'base64', // 数据的类型
68
+ 'savePath': savePath
69
+ }
70
+ postFunc(data)
71
+ }).catch((err) => {
72
+ errFunc({ err: err, file: file.file, chunkIndex: index, fileName: file.file.name })
73
+ })
74
+ }, 10);
75
+ }
76
+ }
77
+ }
78
+
79
+ public static selectFile(options: any = { multiple: true, accept: "*/*" }): Promise<FileList | null> {
10
80
  return new Promise((res: (value: FileList) => void, rej) => {
11
81
  const el = document.createElement("input");
12
82
  el.type = "file";
@@ -29,7 +99,10 @@ export namespace EMacro {
29
99
 
30
100
  public static readLocalExcel(onloadend: (file, workbook) => void, options: any = { multiple: true, accept: "*/*" }) {
31
101
 
32
- this.selectFile(options).then((files: FileList) => {
102
+ this.selectFile(options).then((files: FileList | null) => {
103
+ if (files === null) {
104
+ return
105
+ }
33
106
  for (let i = 0; i < files.length; i++) {
34
107
  const file = files[i];
35
108
  let reader = new FileReader();
@@ -47,8 +120,7 @@ export namespace EMacro {
47
120
  }
48
121
 
49
122
  }
50
-
51
123
 
52
124
 
53
125
 
54
-
126
+