get-file-system-manager.uvue 17.4 KB
Newer Older
1
<template>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  <!-- #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" />
    <button type="primary" @tap="statFileInfoTest" class="btn-stat-file">递归获取目录files的Stats对象{{statFile}}</button>
    <button type="primary" @tap="mkdirTest" class="btn-mkdir">创建文件夹{{mkdirFile}}</button>
    <button type="primary" @tap="writeFileTest" class="btn-write-file">覆盖写入文件{{writeFile}}</button>
    <button type="primary" @tap="readDirTest" class="btn-read-dir">读取文件夹{{readDir}}</button>
    <button type="primary" @tap="readFileTest" class="btn-read-file">读取文件{{readFile}}</button>
    <button type="primary" @tap="copyFileTest" class="btn-copy-file">复制文件{{copyFromFile}}到{{copyToFile}}</button>
    <button type="primary" @tap="renameFileTest"
      class="btn-rename-file">重命名文件{{renameFromFile}}到{{renameToFile}}</button>
    <button type="primary" @tap="accessFileTest" class="btn-access-file">判断文件{{accessFile}}是否存在</button>
    <button type="primary" @tap="getFileInfoTest" class="btn-get-file-info">获取文件信息{{getFileInfoFile}}</button>
    <button type="primary" @tap="unlinkTest" class="btn-unlink-file">删除文件{{unlinkFile}}</button>
    <button type="primary" @tap="copyStaticToFilesTest" class="btn-copyStatic-file">从static目录复制文件到a目录</button>
    <button type="primary" @tap="unlinkAllFileTest" class="btn-clear-file">删除文件夹{{rmDirFile}}下的所有文件</button>
    <button type="primary" @tap="rmdirTest" class="btn-remove-dir">删除文件夹{{rmDirFile}}</button>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
24 25 26
</template>

<script>
27
  export default {
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    data() {
      return {
        log: "",
        /**
         * 自动化测试需要关闭log
         */
        logAble:true,
        fileListSuccess: [] as string[],
        fileListComplete: [] as string[],
        accessFileRet: '',
        lastFailError: new UniError("uni-file-manager", 1300000, "mock error"),
        lastCompleteError: new UniError("uni-file-manager", 1300000, "mock error"),
        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 86 87 88 89
        fileManager.stat({
          path: `${this.basePath}${this.statFile}`,
          recursive: this.recursiveVal,
          success: function (res : StatSuccessResult) {
            if(this.logAble){
              this.log += 'statFileInfoTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('statFileInfoTest success', res)
杜庆泉's avatar
杜庆泉 已提交
90
            this.statsRet = res.stats
91 92 93 94 95 96 97
            console.log('this.statsRet', this.statsRet)
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'statFileInfoTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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 117 118 119 120
          }
        } as StatOptions)
      },

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

        fileManager.getFileInfo({
          filePath: `${this.basePath}${this.getFileInfoFile}`,
          digestAlgorithm: this.getFileInfoAlgorithm,
          success: function (res : GetFileInfoSuccessResult) {
            if(this.logAble){
              this.log += 'getFileInfoTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
121 122
            this.getFileInfoSize = res.size
            this.getFileInfoDigest = res.digest
123 124 125 126 127 128
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'getFileInfoTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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 150 151 152 153 154 155 156 157 158 159
        fileManager.copyFile({
          srcPath: `${this.basePath}${this.copyFromFile}`,
          destPath: `${this.copyToBasePath}${this.copyToFile}`,
          success: function (res : FileManagerSuccessResult) {
            if(this.logAble){
              this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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
          }
        } as CopyFileOptions)
      },
171

172
      renameFileTest: function (e : any) {
173

174
        let fileManager = uni.getFileSystemManager()
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189
        fileManager.rename({
          oldPath: `${this.basePath}${this.renameFromFile}`,
          newPath: `${this.basePath}${this.renameToFile}`,
          success: function (res : FileManagerSuccessResult) {
            if(this.logAble){
              this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'renameFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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 207 208 209 210
          }
        } as RenameOptions)
      },

      readDirTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()
        fileManager.readdir({
          dirPath: `${this.basePath}${this.readDir}`,
          success: function (res : ReadDirSuccessResult) {
            if(this.logAble){
              this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log("success", res)
211
            this.fileListSuccess = res.files
212 213 214 215 216 217
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'readDirTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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
          }
        } as ReadDirOptions)
      },
232 233


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

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        fileManager.writeFile({
          filePath: `${this.basePath}${this.writeFile}`,
          data: this.writeFileContent,
          encoding: this.writeFileEncoding,
          success: function (res : FileManagerSuccessResult) {
            if(this.logAble){
              this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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 274 275 276 277
        fileManager.readFile({
          filePath: `${this.basePath}${this.readFile}`,
          encoding: this.readFileEncoding,
          success: function (res : ReadFileSuccessResult) {
            if(this.logAble){
              this.log += 'readFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
278
            this.readFileRet = res.data
279 280 281 282 283 284
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'readFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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 303 304 305 306 307 308 309 310 311 312
          }
        } as ReadFileOptions)
      },

      rmdirTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()
        fileManager.rmdir({
          dirPath: `${this.basePath}${this.rmDirFile}`,
          recursive: this.recursiveVal,
          success: function (res : FileManagerSuccessResult) {
            if(this.logAble){
              this.log += 'rmdirTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'rmdirTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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
        let fileManager = uni.getFileSystemManager()
329

330 331 332 333 334 335 336 337 338 339 340 341 342 343
        fileManager.mkdir({
          dirPath: `${this.basePath}${this.mkdirFile}`,
          recursive: this.recursiveVal,
          success: function (res : FileManagerSuccessResult) {
            if(this.logAble){
              this.log += 'mkdirTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'mkdirTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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 362 363 364 365
        let fileManager = uni.getFileSystemManager()
        fileManager.access({
          path: `${this.basePath}${this.accessFile}`,
          success: function (res : FileManagerSuccessResult) {
            if(this.logAble){
              this.log += 'accessFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
366
            this.accessFileRet = res.errMsg
367 368 369 370 371 372
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'accessFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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
      },
      unlinkTest: function (e : any) {
386

387
        let fileManager = uni.getFileSystemManager()
388

389 390 391 392 393 394 395 396 397 398 399 400 401
        fileManager.unlink({
          filePath: `${this.basePath}${this.unlinkFile}`,
          success: function (res : FileManagerSuccessResult) {
            if(this.logAble){
              this.log += 'unlinkTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'unlinkTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
          }
        } as UnLinkOptions)
      },
      unlinkAllFileTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()
        fileManager.readdir({
          dirPath: `${this.basePath}${this.rmDirFile}`,
          success: function (res : ReadDirSuccessResult) {
            console.log("success to readdir", res)
            res.files.forEach(element => {
              console.log(element)
              fileManager.unlink({
                filePath: `${this.basePath}${this.rmDirFile}/${element}`,
                success: function (res : FileManagerSuccessResult) {
                  if(this.logAble){
                    this.log += 'unlinkAllFileTest success:' + JSON.stringify(res) + '\n\n'
                  }
                  console.log('success unlink', res)
                },
                fail: function (res : UniError) {
                  if(this.logAble){
                    this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
                  }
                  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)
            });
          },
          fail: function (res : UniError) {
            this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail to readdir', res)
            this.lastFailError = res
          },
          complete: function (res : any) {
            console.log("complete readdir", res)
            this.done = true
            if (res instanceof ReadDirSuccessResult) {
              this.fileListComplete = res.files
            }
            if (res instanceof UniError) {
              this.lastCompleteError = res
            }
          }
        } as ReadDirOptions)
      },
      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) {
            if(this.logAble){
              this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: function (res : UniError) {
            if(this.logAble){
              this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            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>