ci-plus 1.4.9 → 1.5.0

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": "ci-plus",
3
- "version": "1.4.9",
3
+ "version": "1.5.0",
4
4
  "description": "ci组件库",
5
5
  "main": "./index.ts",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  "vue",
13
13
  "element-plus",
14
14
  "ui组件库二次封装",
15
- "utils中新增一个文件下载的工具函数"
15
+ "utils中新增一个文件下载的工具函数2"
16
16
  ],
17
17
  "type": "module",
18
18
  "author": {
@@ -63,6 +63,81 @@ const ajaxBox = {
63
63
  downFile: function (blob: Blob | BlobPart, fileName: string) {
64
64
  downFileFn(blob, fileName);
65
65
  },
66
+
67
+ // 下载文件
68
+ downFileFetch: function (fillAddress, fileName, method, headers, params, cbpercentage) {
69
+ let options = {
70
+ method: method || 'GET',
71
+ }
72
+ // 判断请求是否为get请求,若是get请求,则将params参数拼接在url后面
73
+ if (method === 'GET') {
74
+ const queryString = new URLSearchParams(params).toString()
75
+ fillAddress = fillAddress + '?' + queryString
76
+ }
77
+ if (headers) {
78
+ options['headers'] = headers
79
+ }
80
+ if ((method === 'post' || method === 'POST') && params) {
81
+ options['body'] = JSON.stringify(params)
82
+ }
83
+
84
+ // 定义每次读取的数据块大小(字节)
85
+ let chunkSize = 10240 // 例如,10KB
86
+ // 如果在params中传递了chunkSize参数,则使用其值作为块大小
87
+ if (params && params.chunkSize) {
88
+ chunkSize = params.chunkSize
89
+ }
90
+ // 初始化已下载的字节数
91
+ let downloaded = 0
92
+ // 初始化 Blob 切片起始点
93
+ let start = 0
94
+
95
+ fetch(fillAddress, options)
96
+ .then((res) => {
97
+ // console.log('resssssss: ', res)
98
+ if (!res.ok) {
99
+ throw new Error('Network response was not ok')
100
+ }
101
+ return res.blob()
102
+ })
103
+ .then((blob) => {
104
+ // console.log('blob: ', blob)
105
+ const reader = new FileReader() // 创建 FileReader 对象
106
+ const readBlobInChunks = () => {
107
+ // 创建一个 Blob 切片,大小为 chunkSize
108
+ const chunkBlob = blob.slice(start, start + chunkSize)
109
+ start += chunkSize // 更新起始点
110
+ // 读取 Blob 切片
111
+ reader.readAsArrayBuffer(chunkBlob)
112
+ reader.onload = () => {
113
+ // 累加已下载的字节数
114
+ // downloaded += reader.result.byteLength
115
+ if (typeof reader.result === 'string' || reader.result === null) {
116
+ console.log('reader.result不为 ArrayBuffer');
117
+ // 处理字符串的情况,可能需要转换为 ArrayBuffer 或者其他处理逻辑
118
+ } else {
119
+ downloaded += reader.result.byteLength;
120
+ }
121
+ // 计算下载进度百分比
122
+ const total = blob.size
123
+ const progress = ((downloaded / total) * 100).toFixed(2)
124
+ cbpercentage && cbpercentage(progress) // 如果传递了获取下载进度的回调函数,则调用回调函数传递进度百分比
125
+ console.log(`下载进度: ${progress}%`)
126
+ // 如果还有数据未读取,则继续读取
127
+ if (start < total) {
128
+ readBlobInChunks()
129
+ } else {
130
+ console.log('下载完成.')
131
+ // 处理下载完成的数据,例如将其保存或显示
132
+ // downFileFn(blob, fileName)
133
+ downFileFn(blob, fileName)
134
+ }
135
+ }
136
+ }
137
+
138
+ readBlobInChunks()
139
+ })
140
+ }
66
141
  /*
67
142
  sendJSONP: function (url, callbackName, callback) {
68
143
  const script = document.createElement('script');
@@ -80,22 +155,10 @@ const ajaxBox = {
80
155
  delete window[callbackName];
81
156
  };
82
157
  },
158
+ */
159
+
160
+
83
161
 
84
- downFileFetch: function (fillAddress, fileName, method, headers, params) {
85
- let options = {
86
- method: method || 'GET',
87
- }
88
- if (headers) {
89
- options['headers'] = headers;
90
- }
91
- if ((method === 'post' || method === 'POST') && params) {
92
- options['body'] = JSON.stringify(params)
93
- }
94
- fetch(fillAddress, options).then(res => res.blob()).then((blob) => {
95
- downFileFn(blob, fileName);
96
- });
97
- }
98
- */
99
162
 
100
163
  }
101
164
  export default ajaxBox;