get-file-system-manager.uvue 17.3 KB
Newer Older
1
<template>
2 3 4 5 6
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
  <!-- #endif -->
    <text>显示简易操作日志,详细日志需真机运行查看</text><button size="mini" @click="log=''">清空日志</button>
    <text style="margin: 2px; padding: 2px; border: 1px solid #000000;" :value="log" />
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>
13
    <button type="primary" @tap="renameFileTest"
14 15 16 17 18 19 20
      id="btn-rename-file">重命名文件{{renameFromFile}}到{{renameToFile}}</button>
    <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>
21 22 23
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
24 25 26
</template>

<script>
27 28 29 30 31
  export default {

    data() {
      return {
        log: "",
32 33 34 35
        /**
         * 自动化测试需要关闭log
         */
        logAble:true,
36 37 38
        fileListSuccess: [] as string[],
        fileListComplete: [] as string[],
        accessFileRet: '',
39 40
        lastFailError: new UniError("uni-file-manager", 1300000, "mock error"),
        lastCompleteError: new UniError("uni-file-manager", 1300000, "mock error"),
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        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,
60
        done: false,
61 62 63
        writeFileEncoding: "utf-8",
        readFileEncoding: "utf-8",
        statsRet: [] as Array<FileStats>,
64 65 66 67 68
        /**
         * 待测试的全局环境变量
         */
        basePath: uni.env.USER_DATA_PATH,
        copyToBasePath: uni.env.USER_DATA_PATH,
69 70 71 72 73
        globalTempPath: uni.env.CACHE_PATH,
        globalRootPath: uni.env.SANDBOX_PATH,
        globalUserDataPath: uni.env.USER_DATA_PATH
      }
    },
74 75 76
    onLoad() {
    },

77
    methods: {
78

79 80
      statFileInfoTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()
杜庆泉's avatar
杜庆泉 已提交
81

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

      getFileInfoTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()

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


142
      copyFileTest: function (e : any) {
143

144
        let fileManager = uni.getFileSystemManager()
145

146 147 148 149
        fileManager.copyFile({
          srcPath: `${this.basePath}${this.copyFromFile}`,
          destPath: `${this.copyToBasePath}${this.copyToFile}`,
          success: function (res : FileManagerSuccessResult) {
150 151 152
            if(this.logAble){
              this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            }
153 154
            console.log('success', res)
          },
155
          fail: function (res : UniError) {
156 157 158
            if(this.logAble){
              this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
159
            console.log('fail', res)
160
            this.lastFailError = res
161 162 163
          },
          complete: function (res : any) {
            console.log("complete", res)
164
            this.done = true
165
            if (res instanceof UniError) {
166 167
              this.lastCompleteError = res
            }
168 169 170 171 172 173 174 175 176 177 178 179
          }
        } as CopyFileOptions)
      },

      renameFileTest: function (e : any) {

        let fileManager = uni.getFileSystemManager()

        fileManager.rename({
          oldPath: `${this.basePath}${this.renameFromFile}`,
          newPath: `${this.basePath}${this.renameToFile}`,
          success: function (res : FileManagerSuccessResult) {
180 181 182
            if(this.logAble){
              this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n'
            }
183 184
            console.log('success', res)
          },
185
          fail: function (res : UniError) {
186 187 188
            if(this.logAble){
              this.log += 'renameFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
189
            console.log('fail', res)
190
            this.lastFailError = res
191 192
          },
          complete: function (res : any) {
193
            this.done = true
194 195
            console.log("complete", res)
            if (res instanceof UniError) {
196 197
              this.lastCompleteError = res
            }
198 199 200 201 202 203 204 205 206
          }
        } as RenameOptions)
      },

      readDirTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()
        fileManager.readdir({
          dirPath: `${this.basePath}${this.readDir}`,
          success: function (res : ReadDirSuccessResult) {
207 208 209
            if(this.logAble){
              this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n'
            }
210
            console.log("success", res)
211
            this.fileListSuccess = res.files
212
          },
213
          fail: function (res : UniError) {
214 215 216
            if(this.logAble){
              this.log += 'readDirTest fail:' + JSON.stringify(res) + '\n\n'
            }
217
            console.log('fail', res)
218
            this.lastFailError = res
219 220 221
          },
          complete: function (res : any) {
            console.log("complete", res)
222
            this.done = true
223
            if (res instanceof ReadDirSuccessResult) {
224 225
              this.fileListComplete = res.files
            }
226
            if (res instanceof UniError) {
227 228
              this.lastCompleteError = res
            }
229 230 231 232 233 234 235 236 237 238 239 240
          }
        } as ReadDirOptions)
      },


      writeFileTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()

        fileManager.writeFile({
          filePath: `${this.basePath}${this.writeFile}`,
          data: this.writeFileContent,
          encoding: this.writeFileEncoding,
241
          success: function (res : FileManagerSuccessResult) {
242 243 244
            if(this.logAble){
              this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n'
            }
245 246
            console.log('success', res)
          },
247
          fail: function (res : UniError) {
248 249 250
            if(this.logAble){
              this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
251
            console.log('fail')
252
            this.lastFailError = res
253
          },
254
          complete: function (res : any) {
255
            this.done = true
256 257
            console.log("complete")
            if (res instanceof UniError) {
258 259
              this.lastCompleteError = res
            }
260 261
          }
        } as WriteFileOptions)
262

263
      },
264 265


266
      readFileTest: function (e : any) {
267

268
        let fileManager = uni.getFileSystemManager()
269

270 271 272 273
        fileManager.readFile({
          filePath: `${this.basePath}${this.readFile}`,
          encoding: this.readFileEncoding,
          success: function (res : ReadFileSuccessResult) {
274 275 276
            if(this.logAble){
              this.log += 'readFileTest success:' + JSON.stringify(res) + '\n\n'
            }
277
            console.log('success', res)
278
            this.readFileRet = res.data
279
          },
280
          fail: function (res : UniError) {
281 282 283
            if(this.logAble){
              this.log += 'readFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
284
            console.log('fail', res)
285
            this.lastFailError = res
286 287 288
          },
          complete: function (res : any) {
            console.log("complete", res)
289
            this.done = true
290
            if (res instanceof UniError) {
291 292
              this.lastCompleteError = res
            }
293 294 295 296 297 298 299 300 301 302
          }
        } as ReadFileOptions)
      },

      rmdirTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()
        fileManager.rmdir({
          dirPath: `${this.basePath}${this.rmDirFile}`,
          recursive: this.recursiveVal,
          success: function (res : FileManagerSuccessResult) {
303 304 305
            if(this.logAble){
              this.log += 'rmdirTest success:' + JSON.stringify(res) + '\n\n'
            }
306 307
            console.log('success', res)
          },
308
          fail: function (res : UniError) {
309 310 311
            if(this.logAble){
              this.log += 'rmdirTest fail:' + JSON.stringify(res) + '\n\n'
            }
312
            console.log('fail', res)
313
            this.lastFailError = res
314 315 316
          },
          complete: function (res : any) {
            console.log("complete", res)
317
            this.done = true
318
            if (res instanceof UniError) {
319 320
              this.lastCompleteError = res
            }
321 322 323
          }
        } as RmDirOptions)
      },
324

325
      mkdirTest: function (e : any) {
326 327
        // 准备测试数据

328 329 330 331 332 333
        let fileManager = uni.getFileSystemManager()

        fileManager.mkdir({
          dirPath: `${this.basePath}${this.mkdirFile}`,
          recursive: this.recursiveVal,
          success: function (res : FileManagerSuccessResult) {
334 335 336
            if(this.logAble){
              this.log += 'mkdirTest success:' + JSON.stringify(res) + '\n\n'
            }
337 338
            console.log('success', res)
          },
339
          fail: function (res : UniError) {
340 341 342
            if(this.logAble){
              this.log += 'mkdirTest fail:' + JSON.stringify(res) + '\n\n'
            }
343
            console.log('fail', res)
344
            this.lastFailError = res
345 346 347
          },
          complete: function (res : any) {
            if (res instanceof UniError) {
348 349 350
              this.lastCompleteError = res
            }
            this.done = true
351 352 353
            console.log("complete", res)
          }
        } as MkDirOptions)
354

355 356
      },
      accessFileTest: function (e : any) {
357
        this.accessFileRet = ''
358 359 360 361
        let fileManager = uni.getFileSystemManager()
        fileManager.access({
          path: `${this.basePath}${this.accessFile}`,
          success: function (res : FileManagerSuccessResult) {
362 363 364
            if(this.logAble){
              this.log += 'accessFileTest success:' + JSON.stringify(res) + '\n\n'
            }
365
            console.log('success', res)
366
            this.accessFileRet = res.errMsg
367 368
          },
          fail: function (res : UniError) {
369 370 371
            if(this.logAble){
              this.log += 'accessFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
372
            console.log('fail', res)
373
            this.lastFailError = res
374 375 376
          },
          complete: function (res : any) {
            if (res instanceof UniError) {
377 378
              this.lastCompleteError = res
            }
379
            console.log("complete", res)
380
            this.done = true
381 382
          }
        } as AccessOptions)
383

384 385 386 387 388 389 390 391
      },
      unlinkTest: function (e : any) {

        let fileManager = uni.getFileSystemManager()

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

        let fileManager = uni.getFileSystemManager()

        fileManager.copyFile({
          srcPath: UTSAndroid.getResourcePath("static/list-mock/mock.json"),
          destPath: `${this.copyToBasePath}/a/mock.json`,
          success: function (res : FileManagerSuccessResult) {
471 472 473
            if(this.logAble){
              this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            }
474 475
            console.log('success', res)
          },
476
          fail: function (res : UniError) {
477 478 479
            if(this.logAble){
              this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
480 481 482 483 484 485 486 487 488 489 490 491 492 493
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: function (res : any) {
            console.log("complete", res)
            this.done = true
            if (res instanceof UniError) {
              this.lastCompleteError = res
            }
          }
        } as CopyFileOptions)
      },
    }
  }
494 495 496 497 498 499
</script>

<style>


</style>