get-file-system-manager.uvue 16.1 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-remove-dir">从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 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
  export default {

    data() {
      return {
        log: "",
        fileListSuccess: [] as string[],
        fileListComplete: [] as string[],
        accessFileRet: '',
        lastFailError: UniError("uni-file-manager", 1300000, "mock error"),
        lastCompleteError: 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,
56
        done: false,
57 58 59
        writeFileEncoding: "utf-8",
        readFileEncoding: "utf-8",
        statsRet: [] as Array<FileStats>,
60 61 62 63 64
        /**
         * 待测试的全局环境变量
         */
        basePath: uni.env.USER_DATA_PATH,
        copyToBasePath: uni.env.USER_DATA_PATH,
65 66 67 68 69 70 71
        globalTempPath: uni.env.CACHE_PATH,
        globalRootPath: uni.env.SANDBOX_PATH,
        globalInnerRootPath: uni.env.ANDROID_INTERNAL_SANDBOX_PATH,
        globalAppResourcePath: uni.env.APP_RESOURCE_PATH,
        globalUserDataPath: uni.env.USER_DATA_PATH
      }
    },
72 73 74
    onLoad() {
    },

75
    methods: {
76

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

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

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

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


132
      copyFileTest: function (e : any) {
133

134
        let fileManager = uni.getFileSystemManager()
135

136 137 138 139 140 141 142 143 144 145
        fileManager.copyFile({
          srcPath: `${this.basePath}${this.copyFromFile}`,
          destPath: `${this.copyToBasePath}${this.copyToFile}`,
          success: function (res : FileManagerSuccessResult) {
            this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
          },
          fail: function (res : any) {
            this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail', res)
146
            this.lastFailError = res
147 148 149
          },
          complete: function (res : any) {
            console.log("complete", res)
150
            this.done = true
151
            if (res instanceof UniError) {
152 153
              this.lastCompleteError = res
            }
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
          }
        } 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) {
            this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
          },
          fail: function (res : any) {
            this.log += 'renameFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail', res)
172
            this.lastFailError = res
173 174
          },
          complete: function (res : any) {
175
            this.done = true
176 177
            console.log("complete", res)
            if (res instanceof UniError) {
178 179
              this.lastCompleteError = res
            }
180 181 182 183 184 185 186 187 188 189 190
          }
        } as RenameOptions)
      },

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


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

        fileManager.writeFile({
          filePath: `${this.basePath}${this.writeFile}`,
          data: this.writeFileContent,
          encoding: this.writeFileEncoding,
          success: function (res) {
            this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
          },
          fail: function (res) {
            this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail')
226
            this.lastFailError = res
227 228
          },
          complete: function (res) {
229
            this.done = true
230 231
            console.log("complete")
            if (res instanceof UniError) {
232 233
              this.lastCompleteError = res
            }
234 235
          }
        } as WriteFileOptions)
236

237
      },
238 239


240
      readFileTest: function (e : any) {
241

242
        let fileManager = uni.getFileSystemManager()
243

244 245 246 247 248 249
        fileManager.readFile({
          filePath: `${this.basePath}${this.readFile}`,
          encoding: this.readFileEncoding,
          success: function (res : ReadFileSuccessResult) {
            this.log += 'readFileTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
250
            this.readFileRet = res.data
251 252 253 254
          },
          fail: function (res : any) {
            this.log += 'readFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail', res)
255
            this.lastFailError = res
256 257 258
          },
          complete: function (res : any) {
            console.log("complete", res)
259
            this.done = true
260
            if (res instanceof UniError) {
261 262
              this.lastCompleteError = res
            }
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
          }
        } as ReadFileOptions)
      },

      rmdirTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()
        fileManager.rmdir({
          dirPath: `${this.basePath}${this.rmDirFile}`,
          recursive: this.recursiveVal,
          success: function (res : FileManagerSuccessResult) {
            this.log += 'rmdirTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
          },
          fail: function (res : any) {
            this.log += 'rmdirTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail', res)
279
            this.lastFailError = res
280 281 282
          },
          complete: function (res : any) {
            console.log("complete", res)
283
            this.done = true
284
            if (res instanceof UniError) {
285 286
              this.lastCompleteError = res
            }
287 288 289
          }
        } as RmDirOptions)
      },
290

291
      mkdirTest: function (e : any) {
292 293
        // 准备测试数据

294 295 296 297 298 299 300 301 302 303 304 305
        let fileManager = uni.getFileSystemManager()

        fileManager.mkdir({
          dirPath: `${this.basePath}${this.mkdirFile}`,
          recursive: this.recursiveVal,
          success: function (res : FileManagerSuccessResult) {
            this.log += 'mkdirTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
          },
          fail: function (res : any) {
            this.log += 'mkdirTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail', res)
306
            this.lastFailError = res
307 308 309
          },
          complete: function (res : any) {
            if (res instanceof UniError) {
310 311 312
              this.lastCompleteError = res
            }
            this.done = true
313 314 315
            console.log("complete", res)
          }
        } as MkDirOptions)
316

317 318
      },
      accessFileTest: function (e : any) {
319
        this.accessFileRet = ''
320
        let fileManager = uni.getFileSystemManager()
321

322 323 324 325 326
        fileManager.access({
          path: `${this.basePath}${this.accessFile}`,
          success: function (res : FileManagerSuccessResult) {
            this.log += 'accessFileTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
327
            this.accessFileRet = res.errMsg
328 329 330 331
          },
          fail: function (res : UniError) {
            this.log += 'accessFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail', res)
332
            this.lastFailError = res
333 334 335
          },
          complete: function (res : any) {
            if (res instanceof UniError) {
336 337
              this.lastCompleteError = res
            }
338
            console.log("complete", res)
339
            this.done = true
340 341
          }
        } as AccessOptions)
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356
      },
      unlinkTest: function (e : any) {

        let fileManager = uni.getFileSystemManager()

        fileManager.unlink({
          filePath: `${this.basePath}${this.unlinkFile}`,
          success: function (res : FileManagerSuccessResult) {
            this.log += 'unlinkTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
          },
          fail: function (res : UniError) {
            this.log += 'unlinkTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail', res)
357
            this.lastFailError = res
358 359 360
          },
          complete: function (res : any) {
            if (res instanceof UniError) {
361 362
              this.lastCompleteError = res
            }
363
            console.log("complete", res)
364
            this.done = true
365 366 367 368 369
          }
        } as UnLinkOptions)
      },
      unlinkAllFileTest: function (e : any) {
        let fileManager = uni.getFileSystemManager()
W
wanganxp 已提交
370
        fileManager.readdir({
371 372 373
          dirPath: `${this.basePath}${this.rmDirFile}`,
          success: function (res : ReadDirSuccessResult) {
            console.log("success to readdir", res)
W
wanganxp 已提交
374
            res.files.forEach(element => {
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
              console.log(element)
              fileManager.unlink({
                filePath: `${this.basePath}${this.rmDirFile}/${element}`,
                success: function (res : FileManagerSuccessResult) {
                  this.log += 'unlinkAllFileTest success:' + JSON.stringify(res) + '\n\n'
                  console.log('success unlink', res)
                },
                fail: function (res : UniError) {
                  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)
W
wanganxp 已提交
395
            });
396 397 398 399
          },
          fail: function (res : any) {
            this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail to readdir', res)
W
wanganxp 已提交
400
            this.lastFailError = res
401 402 403
          },
          complete: function (res : any) {
            console.log("complete readdir", res)
W
wanganxp 已提交
404
            this.done = true
405
            if (res instanceof ReadDirSuccessResult) {
W
wanganxp 已提交
406 407
              this.fileListComplete = res.files
            }
408
            if (res instanceof UniError) {
W
wanganxp 已提交
409 410
              this.lastCompleteError = res
            }
411
          }
W
wanganxp 已提交
412 413
        } as ReadDirOptions)
      },
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
      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) {
            this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            console.log('success', res)
          },
          fail: function (res : any) {
            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)
      },
    }
  }
441 442 443 444 445 446
</script>

<style>


</style>