fast-vue-multi-pages 1.0.9 → 1.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,10 +9,10 @@ export declare class FastVueMultiFile {
9
9
  * @param content base64内容
10
10
  * @param fileName 文件名
11
11
  */
12
- static base64ToFile(content: string, fileName: string): File;
12
+ static base64ToFile(content: string, fileName: string): Promise<File>;
13
13
  /**
14
14
  * 将file对象转为base64
15
15
  * @param file
16
16
  */
17
- static fileToBase64(file: File): Promise<unknown>;
17
+ static fileToBase64(file: File): Promise<string>;
18
18
  }
@@ -47,14 +47,23 @@ class FastVueMultiFile {
47
47
  * @param fileName 文件名
48
48
  */
49
49
  static base64ToFile(content, fileName) {
50
- let arr = content.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
51
- while (n--) {
52
- u8arr[n] = bstr.charCodeAt(n);
53
- }
54
- const blob = new Blob([u8arr], { type: mime });
55
- blob.lastModifiedDate = new Date();
56
- blob.name = fileName;
57
- return blob;
50
+ return new Promise(function (resolved, rejected) {
51
+ // 将base64的数据部分提取出来
52
+ const parts = content.split(';base64,');
53
+ const contentType = parts[0].split(':')[1];
54
+ const raw = window.atob(parts[1]);
55
+ // 将原始数据转换为Uint8Array
56
+ const rawLength = raw.length;
57
+ const uInt8Array = new Uint8Array(rawLength);
58
+ for (let i = 0; i < rawLength; ++i) {
59
+ uInt8Array[i] = raw.charCodeAt(i);
60
+ }
61
+ // 使用Blob对象创建File对象
62
+ const blob = new Blob([uInt8Array], { type: contentType });
63
+ blob.lastModifiedDate = new Date();
64
+ blob.name = fileName;
65
+ resolved(new File([blob], fileName, { type: contentType }));
66
+ });
58
67
  }
59
68
  /**
60
69
  * 将file对象转为base64
@@ -9,10 +9,10 @@ export declare class FastVueMultiFile {
9
9
  * @param content base64内容
10
10
  * @param fileName 文件名
11
11
  */
12
- static base64ToFile(content: string, fileName: string): File;
12
+ static base64ToFile(content: string, fileName: string): Promise<File>;
13
13
  /**
14
14
  * 将file对象转为base64
15
15
  * @param file
16
16
  */
17
- static fileToBase64(file: File): Promise<unknown>;
17
+ static fileToBase64(file: File): Promise<string>;
18
18
  }
@@ -57,14 +57,23 @@ define(["require", "exports", "tslib"], function (require, exports, tslib_1) {
57
57
  * @param fileName 文件名
58
58
  */
59
59
  FastVueMultiFile.base64ToFile = function (content, fileName) {
60
- var arr = content.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
61
- while (n--) {
62
- u8arr[n] = bstr.charCodeAt(n);
63
- }
64
- var blob = new Blob([u8arr], { type: mime });
65
- blob.lastModifiedDate = new Date();
66
- blob.name = fileName;
67
- return blob;
60
+ return new Promise(function (resolved, rejected) {
61
+ // 将base64的数据部分提取出来
62
+ var parts = content.split(';base64,');
63
+ var contentType = parts[0].split(':')[1];
64
+ var raw = window.atob(parts[1]);
65
+ // 将原始数据转换为Uint8Array
66
+ var rawLength = raw.length;
67
+ var uInt8Array = new Uint8Array(rawLength);
68
+ for (var i = 0; i < rawLength; ++i) {
69
+ uInt8Array[i] = raw.charCodeAt(i);
70
+ }
71
+ // 使用Blob对象创建File对象
72
+ var blob = new Blob([uInt8Array], { type: contentType });
73
+ blob.lastModifiedDate = new Date();
74
+ blob.name = fileName;
75
+ resolved(new File([blob], fileName, { type: contentType }));
76
+ });
68
77
  };
69
78
  /**
70
79
  * 将file对象转为base64
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-vue-multi-pages",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "author": "janesen",
5
5
  "description": "快速搭建VUE项目工具类的基本库,主要用于每个功能页面独立生成html",
6
6
  "main": "./dist/cjs/index.js",
@@ -31,7 +31,8 @@
31
31
  "@types/js-cookie": "^3.0.6",
32
32
  "@types/lodash": "^4.17.6",
33
33
  "copy-to-clipboard": "^3.3.3",
34
- "ip": "^2.0.1"
34
+ "ip": "^2.0.1",
35
+ "file64": "^1.0.3"
35
36
  },
36
37
  "devDependencies": {
37
38
  "@typescript-eslint/eslint-plugin": "^7.16.0",