get-file-system-manager.uvue 17.1 KB
Newer Older
1
<template>
2
  <!-- #ifdef APP -->
H
hdx 已提交
3
  <scroll-view class="page-scroll-view">
4 5
  <!-- #endif -->
    <text>显示简易操作日志,详细日志需真机运行查看</text><button size="mini" @click="log=''">清空日志</button>
雪洛's avatar
雪洛 已提交
6
    <text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ log }}</text>
7 8 9 10 11 12
    <button type="primary" @tap="statFileInfoTest" id="btn-stat-file">递归获取目录files的Stats对象{{statFile}}</button>
    <button type="primary" @tap="mkdirTest" id="btn-mkdir">创建文件夹{{mkdirFile}}</button>
    <button type="primary" @tap="writeFileTest" id="btn-write-file">覆盖写入文件{{writeFile}}</button>
    <button type="primary" @tap="readDirTest" id="btn-read-dir">读取文件夹{{readDir}}</button>
    <button type="primary" @tap="readFileTest" id="btn-read-file">读取文件{{readFile}}</button>
    <button type="primary" @tap="copyFileTest" id="btn-copy-file">复制文件{{copyFromFile}}到{{copyToFile}}</button>
H
hdx 已提交
13
    <button type="primary" @tap="renameFileTest" id="btn-rename-file">重命名文件{{renameFromFile}}到{{renameToFile}}</button>
14 15 16 17 18 19
    <button type="primary" @tap="accessFileTest" id="btn-access-file">判断文件{{accessFile}}是否存在</button>
    <button type="primary" @tap="getFileInfoTest" id="btn-get-file-info">获取文件信息{{getFileInfoFile}}</button>
    <button type="primary" @tap="unlinkTest" id="btn-unlink-file">删除文件{{unlinkFile}}</button>
    <button type="primary" @tap="copyStaticToFilesTest" id="btn-copyStatic-file">从static目录复制文件到a目录</button>
    <button type="primary" @tap="unlinkAllFileTest" id="btn-clear-file">删除文件夹{{rmDirFile}}下的所有文件</button>
    <button type="primary" @tap="rmdirTest" id="btn-remove-dir">删除文件夹{{rmDirFile}}</button>
20 21 22
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
23 24 25
</template>

<script>
26 27 28 29 30
  export default {

    data() {
      return {
        log: "",
31 32 33
        /**
         * 自动化测试需要关闭log
         */
H
hdx 已提交
34
        logAble: true,
35 36 37
        fileListSuccess: [] as string[],
        fileListComplete: [] as string[],
        accessFileRet: '',
38 39
        lastFailError: new UniError("uni-file-manager", 1300000, "mock error"),
        lastCompleteError: new UniError("uni-file-manager", 1300000, "mock error"),
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        readDir: 'a',
        readFileRet: "",
        writeFileContent: "中文 en.\r\n\t换行",
        getFileInfoAlgorithm: "md5",
        getFileInfoSize: -1,
        getFileInfoDigest: "",
        unlinkFile: 'a/1.txt',
        accessFile: 'a/1.txt',
        writeFile: 'a/1.txt',
        copyFromFile: 'a/1.txt',
        copyToFile: 'a/2.txt',
        renameFromFile: 'a/2.txt',
        renameToFile: 'a/3.txt',
        getFileInfoFile: 'a/1.txt',
        statFile: '',
        rmDirFile: 'a',
        mkdirFile: 'a',
        readFile: 'a/1.txt',
        recursiveVal: true,
59
        done: false,
60 61 62
        writeFileEncoding: "utf-8",
        readFileEncoding: "utf-8",
        statsRet: [] as Array<FileStats>,
63 64 65 66 67
        /**
         * 待测试的全局环境变量
         */
        basePath: uni.env.USER_DATA_PATH,
        copyToBasePath: uni.env.USER_DATA_PATH,
68 69 70 71 72
        globalTempPath: uni.env.CACHE_PATH,
        globalRootPath: uni.env.SANDBOX_PATH,
        globalUserDataPath: uni.env.USER_DATA_PATH
      }
    },
73 74 75
    onLoad() {
    },

76
    methods: {
77
      statFileInfoTest: function (_ : any) {
H
hdx 已提交
78
        const fileManager = uni.getFileSystemManager()
杜庆泉's avatar
杜庆泉 已提交
79

80 81 82
        fileManager.stat({
          path: `${this.basePath}${this.statFile}`,
          recursive: this.recursiveVal,
83
          success: (res : StatSuccessResult) => {
H
hdx 已提交
84
            if (this.logAble) {
85 86
              this.log += 'statFileInfoTest success:' + JSON.stringify(res) + '\n\n'
            }
87
            console.log('statFileInfoTest success', res)
杜庆泉's avatar
杜庆泉 已提交
88
            this.statsRet = res.stats
89 90
            console.log('this.statsRet', this.statsRet)
          },
91
          fail: (res : UniError) => {
H
hdx 已提交
92
            if (this.logAble) {
93 94
              this.log += 'statFileInfoTest fail:' + JSON.stringify(res) + '\n\n'
            }
95
            console.log('statFileInfoTest fail', res)
杜庆泉's avatar
杜庆泉 已提交
96
            this.lastFailError = res
97
          },
98
          complete: (res : any) => {
99
            console.log("statFileInfoTest complete", res)
杜庆泉's avatar
杜庆泉 已提交
100
            this.done = true
101
            if (res instanceof UniError) {
杜庆泉's avatar
杜庆泉 已提交
102 103
              this.lastCompleteError = res
            }
104 105 106 107
          }
        } as StatOptions)
      },

108
      getFileInfoTest: function () {
H
hdx 已提交
109
        const fileManager = uni.getFileSystemManager()
110 111 112 113

        fileManager.getFileInfo({
          filePath: `${this.basePath}${this.getFileInfoFile}`,
          digestAlgorithm: this.getFileInfoAlgorithm,
114
          success: (res : GetFileInfoSuccessResult) => {
H
hdx 已提交
115
            if (this.logAble) {
116 117
              this.log += 'getFileInfoTest success:' + JSON.stringify(res) + '\n\n'
            }
118
            console.log('success', res)
119 120
            this.getFileInfoSize = res.size
            this.getFileInfoDigest = res.digest
121
          },
122
          fail: (res : UniError) => {
H
hdx 已提交
123
            if (this.logAble) {
124 125
              this.log += 'getFileInfoTest fail:' + JSON.stringify(res) + '\n\n'
            }
126
            console.log('fail', res)
127
            this.lastFailError = res
128
          },
129
          complete: (res : any) => {
130
            console.log("complete", res)
131
            this.done = true
132
            if (res instanceof UniError) {
133 134
              this.lastCompleteError = res
            }
135 136 137
          }
        } as GetFileInfoOptions)
      },
138

139
      copyFileTest: function () {
H
hdx 已提交
140
        const fileManager = uni.getFileSystemManager()
141

142 143 144
        fileManager.copyFile({
          srcPath: `${this.basePath}${this.copyFromFile}`,
          destPath: `${this.copyToBasePath}${this.copyToFile}`,
145
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
146
            if (this.logAble) {
147 148
              this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            }
149 150
            console.log('success', res)
          },
151
          fail: (res : UniError) => {
H
hdx 已提交
152
            if (this.logAble) {
153 154
              this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
155
            console.log('fail', res)
156
            this.lastFailError = res
157
          },
158
          complete: (res : any) => {
159
            console.log("complete", res)
160
            this.done = true
161
            if (res instanceof UniError) {
162 163
              this.lastCompleteError = res
            }
164 165 166 167
          }
        } as CopyFileOptions)
      },

168
      renameFileTest: function () {
H
hdx 已提交
169
        const fileManager = uni.getFileSystemManager()
170 171 172 173

        fileManager.rename({
          oldPath: `${this.basePath}${this.renameFromFile}`,
          newPath: `${this.basePath}${this.renameToFile}`,
174
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
175
            if (this.logAble) {
176 177
              this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n'
            }
178 179
            console.log('success', res)
          },
180
          fail: (res : UniError) => {
H
hdx 已提交
181
            if (this.logAble) {
182 183
              this.log += 'renameFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
184
            console.log('fail', res)
185
            this.lastFailError = res
186
          },
187
          complete: (res : any) => {
188
            this.done = true
189 190
            console.log("complete", res)
            if (res instanceof UniError) {
191 192
              this.lastCompleteError = res
            }
193 194 195 196
          }
        } as RenameOptions)
      },

197
      readDirTest: function () {
H
hdx 已提交
198
        const fileManager = uni.getFileSystemManager()
199 200
        fileManager.readdir({
          dirPath: `${this.basePath}${this.readDir}`,
201
          success: (res : ReadDirSuccessResult) => {
H
hdx 已提交
202
            if (this.logAble) {
203 204
              this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n'
            }
205
            console.log("success", res)
206
            this.fileListSuccess = res.files
207
          },
208
          fail: (res : UniError) => {
H
hdx 已提交
209
            if (this.logAble) {
210 211
              this.log += 'readDirTest fail:' + JSON.stringify(res) + '\n\n'
            }
212
            console.log('fail', res)
213
            this.lastFailError = res
214
          },
215
          complete: (res : any) => {
216
            console.log("complete", res)
217
            this.done = true
218
            if (res instanceof ReadDirSuccessResult) {
219 220
              this.fileListComplete = res.files
            }
221
            if (res instanceof UniError) {
222 223
              this.lastCompleteError = res
            }
224 225 226 227
          }
        } as ReadDirOptions)
      },

228
      writeFileTest: function (_ : any) {
H
hdx 已提交
229
        const fileManager = uni.getFileSystemManager()
230 231 232 233 234

        fileManager.writeFile({
          filePath: `${this.basePath}${this.writeFile}`,
          data: this.writeFileContent,
          encoding: this.writeFileEncoding,
235
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
236
            if (this.logAble) {
237 238
              this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n'
            }
239 240
            console.log('success', res)
          },
241
          fail:  (res : UniError) => {
H
hdx 已提交
242
            if (this.logAble) {
243 244
              this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
245
            console.log('fail')
246
            this.lastFailError = res
247
          },
248
          complete: (res : any) => {
249
            this.done = true
250 251
            console.log("complete")
            if (res instanceof UniError) {
252 253
              this.lastCompleteError = res
            }
254 255 256
          }
        } as WriteFileOptions)
      },
257

258
      readFileTest: function () {
H
hdx 已提交
259
        const fileManager = uni.getFileSystemManager()
260

261 262 263
        fileManager.readFile({
          filePath: `${this.basePath}${this.readFile}`,
          encoding: this.readFileEncoding,
264
          success: (res : ReadFileSuccessResult) => {
H
hdx 已提交
265
            if (this.logAble) {
266 267
              this.log += 'readFileTest success:' + JSON.stringify(res) + '\n\n'
            }
268
            console.log('success', res)
269
            this.readFileRet = res.data
270
          },
271
          fail: (res : UniError) => {
H
hdx 已提交
272
            if (this.logAble) {
273 274
              this.log += 'readFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
275
            console.log('fail', res)
276
            this.lastFailError = res
277
          },
278
          complete: (res : any) => {
279
            console.log("complete", res)
280
            this.done = true
281
            if (res instanceof UniError) {
282 283
              this.lastCompleteError = res
            }
284 285 286 287
          }
        } as ReadFileOptions)
      },

288
      rmdirTest: function () {
H
hdx 已提交
289
        const fileManager = uni.getFileSystemManager()
290 291 292
        fileManager.rmdir({
          dirPath: `${this.basePath}${this.rmDirFile}`,
          recursive: this.recursiveVal,
293
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
294
            if (this.logAble) {
295 296
              this.log += 'rmdirTest success:' + JSON.stringify(res) + '\n\n'
            }
297 298
            console.log('success', res)
          },
299
          fail: (res : UniError) => {
H
hdx 已提交
300
            if (this.logAble) {
301 302
              this.log += 'rmdirTest fail:' + JSON.stringify(res) + '\n\n'
            }
303
            console.log('fail', res)
304
            this.lastFailError = res
305
          },
306
          complete: (res : any) => {
307
            console.log("complete", res)
308
            this.done = true
309
            if (res instanceof UniError) {
310 311
              this.lastCompleteError = res
            }
312 313 314
          }
        } as RmDirOptions)
      },
315

316
      mkdirTest: function () {
317
        // 准备测试数据
H
hdx 已提交
318
        const fileManager = uni.getFileSystemManager()
319 320 321 322

        fileManager.mkdir({
          dirPath: `${this.basePath}${this.mkdirFile}`,
          recursive: this.recursiveVal,
323
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
324
            if (this.logAble) {
325 326
              this.log += 'mkdirTest success:' + JSON.stringify(res) + '\n\n'
            }
327 328
            console.log('success', res)
          },
329
          fail: (res : UniError) => {
H
hdx 已提交
330
            if (this.logAble) {
331 332
              this.log += 'mkdirTest fail:' + JSON.stringify(res) + '\n\n'
            }
333
            console.log('fail', res)
334
            this.lastFailError = res
335
          },
336
          complete: (res : any) => {
337
            if (res instanceof UniError) {
338 339 340
              this.lastCompleteError = res
            }
            this.done = true
341 342 343
            console.log("complete", res)
          }
        } as MkDirOptions)
344

345
      },
346
      accessFileTest: function () {
347
        this.accessFileRet = ''
H
hdx 已提交
348
        const fileManager = uni.getFileSystemManager()
349 350
        fileManager.access({
          path: `${this.basePath}${this.accessFile}`,
351
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
352
            if (this.logAble) {
353 354
              this.log += 'accessFileTest success:' + JSON.stringify(res) + '\n\n'
            }
355
            console.log('success', res)
356
            this.accessFileRet = res.errMsg
357
          },
358
          fail: (res : UniError) => {
H
hdx 已提交
359
            if (this.logAble) {
360 361
              this.log += 'accessFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
362
            console.log('fail', res)
363
            this.lastFailError = res
364
          },
365
          complete: (res : any) => {
366
            if (res instanceof UniError) {
367 368
              this.lastCompleteError = res
            }
369
            console.log("complete", res)
370
            this.done = true
371 372
          }
        } as AccessOptions)
373

374
      },
375
      unlinkTest: function () {
H
hdx 已提交
376
        const fileManager = uni.getFileSystemManager()
377 378 379

        fileManager.unlink({
          filePath: `${this.basePath}${this.unlinkFile}`,
380
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
381
            if (this.logAble) {
382 383
              this.log += 'unlinkTest success:' + JSON.stringify(res) + '\n\n'
            }
384 385
            console.log('success', res)
          },
386
          fail: (res : UniError) => {
H
hdx 已提交
387
            if (this.logAble) {
388 389
              this.log += 'unlinkTest fail:' + JSON.stringify(res) + '\n\n'
            }
390
            console.log('fail', res)
391
            this.lastFailError = res
392
          },
393
          complete: (res : any) => {
394
            if (res instanceof UniError) {
395 396
              this.lastCompleteError = res
            }
397
            console.log("complete", res)
398
            this.done = true
399 400 401
          }
        } as UnLinkOptions)
      },
402
      unlinkAllFileTest: function () {
H
hdx 已提交
403
        const fileManager = uni.getFileSystemManager()
W
wanganxp 已提交
404
        fileManager.readdir({
405
          dirPath: `${this.basePath}${this.rmDirFile}`,
406
          success: (res : ReadDirSuccessResult) => {
407
            console.log("success to readdir", res)
W
wanganxp 已提交
408
            res.files.forEach(element => {
409 410 411
              console.log(element)
              fileManager.unlink({
                filePath: `${this.basePath}${this.rmDirFile}/${element}`,
412
                success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
413
                  if (this.logAble) {
414 415
                    this.log += 'unlinkAllFileTest success:' + JSON.stringify(res) + '\n\n'
                  }
416 417
                  console.log('success unlink', res)
                },
418
                fail: (res : UniError) => {
H
hdx 已提交
419
                  if (this.logAble) {
420 421
                    this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
                  }
422 423 424
                  console.log('fail unlink', res)
                  this.lastFailError = res
                },
425
                complete : (res : any) => {
426 427 428 429 430 431 432
                  if (res instanceof UniError) {
                    this.lastCompleteError = res
                  }
                  console.log("complete unlink", res)
                  this.done = true
                }
              } as UnLinkOptions)
W
wanganxp 已提交
433
            });
434
          },
435
          fail : (res : UniError) => {
436 437
            this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail to readdir', res)
W
wanganxp 已提交
438
            this.lastFailError = res
439
          },
440
          complete : (res : any) => {
441
            console.log("complete readdir", res)
W
wanganxp 已提交
442
            this.done = true
443
            if (res instanceof ReadDirSuccessResult) {
W
wanganxp 已提交
444 445
              this.fileListComplete = res.files
            }
446
            if (res instanceof UniError) {
W
wanganxp 已提交
447 448
              this.lastCompleteError = res
            }
449
          }
W
wanganxp 已提交
450 451
        } as ReadDirOptions)
      },
452
      copyStaticToFilesTest: function () {
H
hdx 已提交
453
        const fileManager = uni.getFileSystemManager()
454 455

        fileManager.copyFile({
456
          srcPath: "/static/list-mock/mock.json",
457
          destPath: `${this.copyToBasePath}/a/mock.json`,
458
          success: (res : FileManagerSuccessResult) =>   {
H
hdx 已提交
459
            if (this.logAble) {
460 461
              this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            }
462 463
            console.log('success', res)
          },
464
          fail: (res : UniError) => {
H
hdx 已提交
465
            if (this.logAble) {
466 467
              this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
468 469 470
            console.log('fail', res)
            this.lastFailError = res
          },
471
          complete: (res : any) => {
472 473 474 475 476 477 478 479 480 481
            console.log("complete", res)
            this.done = true
            if (res instanceof UniError) {
              this.lastCompleteError = res
            }
          }
        } as CopyFileOptions)
      },
    }
  }
482 483 484 485 486
</script>

<style>

</style>