get-file-system-manager.uvue 44.3 KB
Newer Older
1
<template>
2
  <!-- #ifdef APP -->
W
WOSHIMAHAIFENG 已提交
3
  <scroll-view style="flex: 1;">
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>
W
WOSHIMAHAIFENG 已提交
20 21 22 23 24 25 26 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 56 57 58




    <button type="primary" @tap="statFileInfoSyncTest"
      id="btn-stat-file-sync">同步递归获取目录files的Stats对象{{statFile}}</button>
    <button type="primary" @tap="appendFileTest" id="btn-append-file">在文件{{readFile}}结尾追加内容</button>
    <button type="primary" @tap="appendFileSyncTest" id="btn-append-file-sync">同步在文件{{readFile}}结尾追加内容</button>
    <button type="primary" @tap="writeFileSyncTest" id="btn-write-file-sync">同步覆盖写入文件{{writeFile}}</button>
    <button type="primary" @tap="readFileSyncTest" id="btn-read-file-sync">同步读取文件{{readFile}}</button>
    <button type="primary" @tap="unlinkSyncTest" id="btn-unlink-file-sync">同步删除文件{{unlinkFile}}</button>
    <button type="primary" @tap="mkdirSyncTest" id="btn-mkdir-sync">同步创建文件夹{{mkdirFile}}</button>
    <button type="primary" @tap="rmdirSyncTest" id="btn-remove-dir-sync">同步删除文件夹{{rmDirFile}}</button>
    <button type="primary" @tap="readDirSyncTest" id="btn-read-dir-sync">同步读取文件夹{{readDir}}</button>
    <button type="primary" @tap="accessFileSyncTest" id="btn-access-file-sync">同步判断文件{{accessFile}}是否存在</button>
    <button type="primary" @tap="renameFileSync"
      id="btn-rename-file-sync">同步重命名文件{{renameFromFile}}到{{renameToFile}}</button>
    <button type="primary" @tap="copyFileSyncTest"
      id="btn-copy-file-sync">同步复制文件{{copyFromFile}}到{{copyToFile}}</button>
    <button type="primary" @tap="saveFileTest" id="btn-save-file">保存临时文件到本地</button>
    <button type="primary" @tap="saveFileSyncTest" id="btn-save-file-sync">同步保存临时文件到本地</button>
    <button type="primary" @tap="removeSavedFileTest" id="btn-remove-saved-file">删除已保存的本地文件</button>
    <button type="primary" @tap="unzipFileTest" id="btn-unzip-file-sync">解压文件</button>
    <button type="primary" @tap="getSavedFileListTest" id="btn-getsaved-filelist">获取该已保存的本地缓存文件列表</button>
    <button type="primary" @tap="truncateFileTest" id="btn-truncate-file">对文件{{writeFile}}内容进行截断操作</button>
    <button type="primary" @tap="truncateFileSyncTest" id="btn-truncate-file-sync">同步对文件{{writeFile}}内容进行截断操作</button>
    <button type="primary" @tap="readCompressedFileTest" id="btn-compressed-file">读取指定压缩类型的本地文件内容</button>
    <button type="primary" @tap="readCompressedFileSyncTest" id="btn-compressed-file-sync">同步读取指定压缩类型的本地文件内容</button>
    <button type="primary" @tap="openFileTest" id="btn-open-file">打开文件{{readFile}},返回描述符</button>
    <button type="primary" @tap="openFileSyncTest" id="btn-open-file-sync">同步打开文件{{readFile}},返回描述符</button>
    <button type="primary" @tap="closeTest" id="btn-close-file">通过文件描述符关闭文件{{readFile}}</button>
    <button type="primary" @tap="closeSyncTest" id="btn-close-file-sync">通过文件描述符同步关闭文件{{readFile}}</button>
    <button type="primary" @tap="writeTest" id="btn-write">通过文件描述符写入文件{{readFile}}</button>
    <button type="primary" @tap="writeSyncTest" id="btn-read-file">同步通过文件描述符写入文件{{readFile}}</button>
    <button type="primary" @tap="fstatTest" id="btn-fstat-file">通过文件描述符获取{{statFile}}的状态信息</button>
    <button type="primary" @tap="fstatSyncTest" id="btn-fstat-file-sync">同步通过文件描述符获取{{statFile}}的状态信息</button>
    <button type="primary" @tap="ftruncateFileTest" id="btn-ftruncate-file">通过文件描述符对文件{{writeFile}}内容进行截断操作</button>
    <button type="primary" @tap="ftruncateFileSyncTest"
      id="btn-ftruncate-file-sync">同步通过文件描述符对文件{{writeFile}}内容进行截断操作</button>
59 60 61
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
62 63 64
</template>

<script>
65 66 67 68 69
  export default {

    data() {
      return {
        log: "",
70 71 72
        /**
         * 自动化测试需要关闭log
         */
H
hdx 已提交
73
        logAble: true,
74 75 76
        fileListSuccess: [] as string[],
        fileListComplete: [] as string[],
        accessFileRet: '',
77 78
        lastFailError: new UniError("uni-file-manager", 1300000, "mock error"),
        lastCompleteError: new UniError("uni-file-manager", 1300000, "mock error"),
79 80 81
        readDir: 'a',
        readFileRet: "",
        writeFileContent: "中文 en.\r\n\t换行",
W
WOSHIMAHAIFENG 已提交
82
        appendFileContent: "append content",
83 84 85 86 87 88
        getFileInfoAlgorithm: "md5",
        getFileInfoSize: -1,
        getFileInfoDigest: "",
        unlinkFile: 'a/1.txt',
        accessFile: 'a/1.txt',
        writeFile: 'a/1.txt',
W
WOSHIMAHAIFENG 已提交
89 90 91
        writeData: 'insert data哈哈哈',
        brFile: 'a/1.txt.br',
        temFile: 'a/1.txt',
92 93 94 95 96 97 98 99 100 101
        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,
102
        done: false,
103 104 105
        writeFileEncoding: "utf-8",
        readFileEncoding: "utf-8",
        statsRet: [] as Array<FileStats>,
W
WOSHIMAHAIFENG 已提交
106 107 108 109 110 111 112 113 114 115
        unzipFile: 'zip/1.zip',
        targetZip: "unzip",
        renameFileRet: '',
        saveFileRet: '',
        removeSavedFileRet: '',
        fd: '',
        closeFileRet: '',
        bytesWritten: 0,
        fstat: new Stats(),
        ftruncateRet:'',
116 117 118 119 120
        /**
         * 待测试的全局环境变量
         */
        basePath: uni.env.USER_DATA_PATH,
        copyToBasePath: uni.env.USER_DATA_PATH,
121 122 123 124 125
        globalTempPath: uni.env.CACHE_PATH,
        globalRootPath: uni.env.SANDBOX_PATH,
        globalUserDataPath: uni.env.USER_DATA_PATH
      }
    },
126 127 128
    onLoad() {
    },

129
    methods: {
130
      statFileInfoTest: function (_ : any) {
H
hdx 已提交
131
        const fileManager = uni.getFileSystemManager()
132
        fileManager.stat({
W
wanganxp 已提交
133 134
          // path: `${this.basePath}${this.statFile}`, //USER_DATA_PATH
          path: `${this.globalTempPath}${this.statFile}`, //CACHE_PATH
135
          recursive: this.recursiveVal,
136
          success: (res : StatSuccessResult) => {
H
hdx 已提交
137
            if (this.logAble) {
138 139
              this.log += 'statFileInfoTest success:' + JSON.stringify(res) + '\n\n'
            }
140
            console.log('statFileInfoTest success', res)
杜庆泉's avatar
杜庆泉 已提交
141
            this.statsRet = res.stats
142 143
            console.log('this.statsRet', this.statsRet)
          },
144
          fail: (res : UniError) => {
H
hdx 已提交
145
            if (this.logAble) {
146 147
              this.log += 'statFileInfoTest fail:' + JSON.stringify(res) + '\n\n'
            }
148
            console.log('statFileInfoTest fail', res)
杜庆泉's avatar
杜庆泉 已提交
149
            this.lastFailError = res
150
          },
151
          complete: (res : any) => {
152
            console.log("statFileInfoTest complete", res)
杜庆泉's avatar
杜庆泉 已提交
153
            this.done = true
154
            if (res instanceof UniError) {
杜庆泉's avatar
杜庆泉 已提交
155 156
              this.lastCompleteError = res
            }
157 158 159 160
          }
        } as StatOptions)
      },

161
      getFileInfoTest: function () {
H
hdx 已提交
162
        const fileManager = uni.getFileSystemManager()
163 164 165 166

        fileManager.getFileInfo({
          filePath: `${this.basePath}${this.getFileInfoFile}`,
          digestAlgorithm: this.getFileInfoAlgorithm,
167
          success: (res : GetFileInfoSuccessResult) => {
H
hdx 已提交
168
            if (this.logAble) {
169 170
              this.log += 'getFileInfoTest success:' + JSON.stringify(res) + '\n\n'
            }
171
            console.log('success', res)
172 173
            this.getFileInfoSize = res.size
            this.getFileInfoDigest = res.digest
174
          },
175
          fail: (res : UniError) => {
H
hdx 已提交
176
            if (this.logAble) {
177 178
              this.log += 'getFileInfoTest fail:' + JSON.stringify(res) + '\n\n'
            }
179
            console.log('fail', res)
180
            this.lastFailError = res
181
          },
182
          complete: (res : any) => {
183
            console.log("complete", res)
184
            this.done = true
185
            if (res instanceof UniError) {
186 187
              this.lastCompleteError = res
            }
188 189 190
          }
        } as GetFileInfoOptions)
      },
191

192
      copyFileTest: function () {
H
hdx 已提交
193
        const fileManager = uni.getFileSystemManager()
194

195 196 197
        fileManager.copyFile({
          srcPath: `${this.basePath}${this.copyFromFile}`,
          destPath: `${this.copyToBasePath}${this.copyToFile}`,
198
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
199
            if (this.logAble) {
200 201
              this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            }
202 203
            console.log('success', res)
          },
204
          fail: (res : UniError) => {
H
hdx 已提交
205
            if (this.logAble) {
206 207
              this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
208
            console.log('fail', res)
209
            this.lastFailError = res
210
          },
211
          complete: (res : any) => {
212
            console.log("complete", res)
213
            this.done = true
214
            if (res instanceof UniError) {
215 216
              this.lastCompleteError = res
            }
217 218 219 220
          }
        } as CopyFileOptions)
      },

221
      renameFileTest: function () {
H
hdx 已提交
222
        const fileManager = uni.getFileSystemManager()
223 224 225 226

        fileManager.rename({
          oldPath: `${this.basePath}${this.renameFromFile}`,
          newPath: `${this.basePath}${this.renameToFile}`,
W
WOSHIMAHAIFENG 已提交
227
          success: (res) => {
H
hdx 已提交
228
            if (this.logAble) {
229 230
              this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n'
            }
231 232
            console.log('success', res)
          },
233
          fail: (res : UniError) => {
H
hdx 已提交
234
            if (this.logAble) {
235 236
              this.log += 'renameFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
237
            console.log('fail', res)
238
            this.lastFailError = res
239
          },
240
          complete: (res : any) => {
241
            this.done = true
242 243
            console.log("complete", res)
            if (res instanceof UniError) {
244 245
              this.lastCompleteError = res
            }
246 247 248 249
          }
        } as RenameOptions)
      },

250
      readDirTest: function () {
H
hdx 已提交
251
        const fileManager = uni.getFileSystemManager()
252 253
        fileManager.readdir({
          dirPath: `${this.basePath}${this.readDir}`,
254
          success: (res : ReadDirSuccessResult) => {
H
hdx 已提交
255
            if (this.logAble) {
256 257
              this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n'
            }
258
            console.log("success", res)
259
            this.fileListSuccess = res.files
260
          },
261
          fail: (res : UniError) => {
H
hdx 已提交
262
            if (this.logAble) {
263 264
              this.log += 'readDirTest fail:' + JSON.stringify(res) + '\n\n'
            }
265
            console.log('fail', res)
266
            this.lastFailError = res
267
          },
268
          complete: (res : any) => {
269
            console.log("complete", res)
270
            this.done = true
271
            if (res instanceof UniError) {
272
              this.lastCompleteError = res
273 274
            } else {
              this.fileListComplete = (res as ReadDirSuccessResult).files
275
            }
276 277 278 279
          }
        } as ReadDirOptions)
      },

280
      writeFileTest: function (_ : any) {
H
hdx 已提交
281
        const fileManager = uni.getFileSystemManager()
282 283 284 285 286

        fileManager.writeFile({
          filePath: `${this.basePath}${this.writeFile}`,
          data: this.writeFileContent,
          encoding: this.writeFileEncoding,
W
WOSHIMAHAIFENG 已提交
287
          success: (res) => {
H
hdx 已提交
288
            if (this.logAble) {
289 290
              this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n'
            }
291 292
            console.log('success', res)
          },
W
WOSHIMAHAIFENG 已提交
293
          fail: (res : UniError) => {
H
hdx 已提交
294
            if (this.logAble) {
295 296
              this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
297
            console.log('fail')
298
            this.lastFailError = res
299
          },
300
          complete: (res : any) => {
301
            this.done = true
302 303
            console.log("complete")
            if (res instanceof UniError) {
304 305
              this.lastCompleteError = res
            }
W
WOSHIMAHAIFENG 已提交
306

307 308 309
          }
        } as WriteFileOptions)
      },
310

311
      readFileTest: function () {
H
hdx 已提交
312
        const fileManager = uni.getFileSystemManager()
313

314 315 316
        fileManager.readFile({
          filePath: `${this.basePath}${this.readFile}`,
          encoding: this.readFileEncoding,
317
          success: (res : ReadFileSuccessResult) => {
H
hdx 已提交
318
            if (this.logAble) {
319 320
              this.log += 'readFileTest success:' + JSON.stringify(res) + '\n\n'
            }
321
            console.log('success', res)
322
            this.readFileRet = res.data
323
          },
324
          fail: (res : UniError) => {
H
hdx 已提交
325
            if (this.logAble) {
326 327
              this.log += 'readFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
328
            console.log('fail', res)
329
            this.lastFailError = res
330
          },
331
          complete: (res : any) => {
332
            console.log("complete", res)
333
            this.done = true
334
            if (res instanceof UniError) {
335 336
              this.lastCompleteError = res
            }
337 338 339 340
          }
        } as ReadFileOptions)
      },

341
      rmdirTest: function () {
H
hdx 已提交
342
        const fileManager = uni.getFileSystemManager()
343 344 345
        fileManager.rmdir({
          dirPath: `${this.basePath}${this.rmDirFile}`,
          recursive: this.recursiveVal,
346
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
347
            if (this.logAble) {
348 349
              this.log += 'rmdirTest success:' + JSON.stringify(res) + '\n\n'
            }
350 351
            console.log('success', res)
          },
352
          fail: (res : UniError) => {
H
hdx 已提交
353
            if (this.logAble) {
354 355
              this.log += 'rmdirTest fail:' + JSON.stringify(res) + '\n\n'
            }
356
            console.log('fail', res)
357
            this.lastFailError = res
358
          },
359
          complete: (res : any) => {
360
            console.log("complete", res)
361
            this.done = true
362
            if (res instanceof UniError) {
363 364
              this.lastCompleteError = res
            }
365 366 367
          }
        } as RmDirOptions)
      },
368

369
      mkdirTest: function () {
370
        // 准备测试数据
H
hdx 已提交
371
        const fileManager = uni.getFileSystemManager()
372 373 374 375

        fileManager.mkdir({
          dirPath: `${this.basePath}${this.mkdirFile}`,
          recursive: this.recursiveVal,
376
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
377
            if (this.logAble) {
378 379
              this.log += 'mkdirTest success:' + JSON.stringify(res) + '\n\n'
            }
380 381
            console.log('success', res)
          },
382
          fail: (res : UniError) => {
H
hdx 已提交
383
            if (this.logAble) {
384 385
              this.log += 'mkdirTest fail:' + JSON.stringify(res) + '\n\n'
            }
386
            console.log('fail', res)
387
            this.lastFailError = res
388
          },
389
          complete: (res : any) => {
390
            if (res instanceof UniError) {
391 392 393
              this.lastCompleteError = res
            }
            this.done = true
394 395 396
            console.log("complete", res)
          }
        } as MkDirOptions)
397

398
      },
399
      accessFileTest: function () {
400
        this.accessFileRet = ''
H
hdx 已提交
401
        const fileManager = uni.getFileSystemManager()
402 403
        fileManager.access({
          path: `${this.basePath}${this.accessFile}`,
404
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
405
            if (this.logAble) {
406 407
              this.log += 'accessFileTest success:' + JSON.stringify(res) + '\n\n'
            }
408
            console.log('success', res)
409
            this.accessFileRet = res.errMsg
410
          },
411
          fail: (res : UniError) => {
H
hdx 已提交
412
            if (this.logAble) {
413 414
              this.log += 'accessFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
415
            console.log('fail', res)
416
            this.lastFailError = res
417
          },
418
          complete: (res : any) => {
419
            if (res instanceof UniError) {
420 421
              this.lastCompleteError = res
            }
422
            console.log("complete", res)
423
            this.done = true
424 425
          }
        } as AccessOptions)
426

427
      },
428
      unlinkTest: function () {
H
hdx 已提交
429
        const fileManager = uni.getFileSystemManager()
430 431 432

        fileManager.unlink({
          filePath: `${this.basePath}${this.unlinkFile}`,
433
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
434
            if (this.logAble) {
435 436
              this.log += 'unlinkTest success:' + JSON.stringify(res) + '\n\n'
            }
437 438
            console.log('success', res)
          },
439
          fail: (res : UniError) => {
H
hdx 已提交
440
            if (this.logAble) {
441 442
              this.log += 'unlinkTest fail:' + JSON.stringify(res) + '\n\n'
            }
443
            console.log('fail', res)
444
            this.lastFailError = res
445
          },
446
          complete: (res : any) => {
447
            if (res instanceof UniError) {
448 449
              this.lastCompleteError = res
            }
450
            console.log("complete", res)
451
            this.done = true
452 453 454
          }
        } as UnLinkOptions)
      },
455
      unlinkAllFileTest: function () {
H
hdx 已提交
456
        const fileManager = uni.getFileSystemManager()
W
wanganxp 已提交
457
        fileManager.readdir({
458
          dirPath: `${this.basePath}${this.rmDirFile}`,
459
          success: (res : ReadDirSuccessResult) => {
460
            console.log("success to readdir", res)
W
wanganxp 已提交
461
            res.files.forEach(element => {
462 463 464
              console.log(element)
              fileManager.unlink({
                filePath: `${this.basePath}${this.rmDirFile}/${element}`,
465
                success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
466
                  if (this.logAble) {
467 468
                    this.log += 'unlinkAllFileTest success:' + JSON.stringify(res) + '\n\n'
                  }
469 470
                  console.log('success unlink', res)
                },
471
                fail: (res : UniError) => {
H
hdx 已提交
472
                  if (this.logAble) {
473 474
                    this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
                  }
475 476 477
                  console.log('fail unlink', res)
                  this.lastFailError = res
                },
W
WOSHIMAHAIFENG 已提交
478
                complete: (res : any) => {
479 480 481 482 483 484 485
                  if (res instanceof UniError) {
                    this.lastCompleteError = res
                  }
                  console.log("complete unlink", res)
                  this.done = true
                }
              } as UnLinkOptions)
W
wanganxp 已提交
486
            });
487
          },
W
WOSHIMAHAIFENG 已提交
488
          fail: (res : UniError) => {
489 490
            this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
            console.log('fail to readdir', res)
W
wanganxp 已提交
491
            this.lastFailError = res
492
          },
W
WOSHIMAHAIFENG 已提交
493
          complete: (res : any) => {
494
            console.log("complete readdir", res)
W
wanganxp 已提交
495
            this.done = true
496
            if (res instanceof UniError) {
W
wanganxp 已提交
497
              this.lastCompleteError = res
498 499
            } else {
              this.fileListComplete = (res as ReadDirSuccessResult).files
W
wanganxp 已提交
500
            }
501
          }
W
wanganxp 已提交
502 503
        } as ReadDirOptions)
      },
504
      copyStaticToFilesTest: function () {
H
hdx 已提交
505
        const fileManager = uni.getFileSystemManager()
506 507

        fileManager.copyFile({
508
          srcPath: "/static/list-mock/mock.json",
509
          destPath: `${this.copyToBasePath}/a/mock.json`,
W
WOSHIMAHAIFENG 已提交
510
          success: (res : FileManagerSuccessResult) => {
H
hdx 已提交
511
            if (this.logAble) {
512 513
              this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
            }
514 515
            console.log('success', res)
          },
516
          fail: (res : UniError) => {
H
hdx 已提交
517
            if (this.logAble) {
518 519
              this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
520 521 522
            console.log('fail', res)
            this.lastFailError = res
          },
523
          complete: (res : any) => {
524 525 526 527 528 529 530 531
            console.log("complete", res)
            this.done = true
            if (res instanceof UniError) {
              this.lastCompleteError = res
            }
          }
        } as CopyFileOptions)
      },
W
WOSHIMAHAIFENG 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
      //start
      appendFileTest: function (_ : any) {
        const fileManager = uni.getFileSystemManager()
        fileManager.appendFile({
          filePath: `${this.basePath}${this.writeFile}`,
          data: this.appendFileContent,
          encoding: this.writeFileEncoding,
          success: (res : FileManagerSuccessResult) => {
            if (this.logAble) {
              this.log += 'appendFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'appendFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail')
            this.lastFailError = res
          },
          complete: (res : any) => {
            this.done = true
            console.log("complete")
            if (res instanceof UniError) {
              this.lastCompleteError = res
            }

          }
        } as AppendFileOptions)
      },
      writeFileSyncTest: function (_ : any) {
        try {
          const fileManager = uni.getFileSystemManager()
          fileManager.writeFileSync(`${this.basePath}${this.writeFile}`, this.writeFileContent, this.writeFileEncoding)
          if (this.logAble) {
            this.log += 'writeFileSyncTest success:' + '\n\n'
          }
          this.done = true
        } catch (e) {
          if (this.logAble) {
            this.log += 'writeFileSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }

      },
      readFileSyncTest: function () {
        try {
          const fileManager = uni.getFileSystemManager()
          let data = fileManager.readFileSync(
            `${this.basePath}${this.readFile}`,
            this.readFileEncoding)
          if (this.logAble) {
            this.log += 'readFileSyncTest result:' + data + '\n\n'
          }
          this.done = true
          this.readFileRet = data
        } catch (e) {
          if (this.logAble) {
            this.log += 'readFileSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }

      },
      unlinkSyncTest: function () {
        try {
          const fileManager = uni.getFileSystemManager()
          fileManager.unlinkSync(
            `${this.basePath}${this.unlinkFile}`)
          this.done = true
        } catch (e) {
          if (this.logAble) {
            this.log += 'unlinkSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }
      },
      mkdirSyncTest: function () {
        // 准备测试数据
        try {
          const fileManager = uni.getFileSystemManager()
          fileManager.mkdirSync(`${this.basePath}${this.mkdirFile}`, this.recursiveVal)
          this.done = true
        } catch (e) {
          this.done = true
          if (this.logAble) {
            this.log += 'mkdirSyncTest fail:' + e + '\n\n'
          }
        }

      },
      rmdirSyncTest: function () {
        try {
          const fileManager = uni.getFileSystemManager()
          fileManager.rmdirSync(
            `${this.basePath}${this.rmDirFile}`,
            this.recursiveVal)
          this.done = true
        } catch (e) {
          if (this.logAble) {
            this.log += 'rmdirSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }
      },
      readDirSyncTest: function () {
        try {
          const fileManager = uni.getFileSystemManager()
          let res = fileManager.readdirSync(
            `${this.basePath}${this.readDir}`)
          if (this.logAble) {
            this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n'
          }
          if (res != null) {
            this.fileListSuccess = res
          }
          this.done = true
        } catch (e) {
          if (this.logAble) {
            this.log += 'rmdirSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }
      },
      accessFileSyncTest: function () {
        this.accessFileRet = ''
        const fileManager = uni.getFileSystemManager()
        try {
          fileManager.accessSync(`${this.basePath}${this.accessFile}`)
          this.done = true
          this.accessFileRet = 'access:ok'
        } catch (e) {
          if (this.logAble) {
            this.log += 'rmdirSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }
      },
      renameFileSync: function () {
        const fileManager = uni.getFileSystemManager()
        try {
          fileManager.renameSync(`${this.basePath}${this.renameFromFile}`,
            `${this.basePath}${this.renameToFile}`)
          this.done = true
          this.renameFileRet = "rename:ok"

        } catch (e) {
          if (this.logAble) {
            this.log += 'rmdirSyncTest fail:' + e + '\n\n'
          }
          console.log('renameSync:' + e)
          this.done = true
        }
      },
      copyFileSyncTest: function () {
        const fileManager = uni.getFileSystemManager()
        try {
          fileManager.copyFileSync(
            `${this.basePath}${this.copyFromFile}`,
            `${this.copyToBasePath}${this.copyToFile}`)
          this.done = true
        } catch (e) {
          if (this.logAble) {
            this.log += 'rmdirSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }
      },
      appendFileSyncTest: function (_ : any) {
        const fileManager = uni.getFileSystemManager()
        try {
          fileManager.appendFileSync(
            `${this.basePath}${this.writeFile}`,
            this.appendFileContent,
            this.writeFileEncoding)
          this.done = true
        } catch (e) {
          if (this.logAble) {
            this.log += 'rmdirSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }
      },
      saveFileTest: function (_ : any) {
        const fileManager = uni.getFileSystemManager()
        console.log('=====' + uni.env.USER_DATA_PATH)
        fileManager.saveFile({
          tempFilePath: `${this.globalTempPath}${this.temFile}`,
          // filePath:`${this.basePath}local/`,
          success: (res : SaveFileSuccessResult) => {
            if (this.logAble) {
              this.log += 'saveFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
            this.saveFileRet = res.savedFilePath
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'saveFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: (res) => {
            this.done = true
          }
        } as SaveFileOptions)
      },
      saveFileSyncTest: function (_ : any) {
        const fileManager = uni.getFileSystemManager()
        try {
          fileManager.saveFileSync(
            `${this.globalTempPath}${this.temFile}`, `${this.basePath}local/`)
          // filePath:`${this.basePath}local/`,)
          this.done = true

          //todo 后面打开
          // this.saveFileRet=res
        } catch (e) {
          console.log(e)
          this.done = true
        }
      },
      unzipFileTest: function (_ : any) {
        const fileManager = uni.getFileSystemManager()

        fileManager.unzip({
          zipFilePath: `${this.basePath}${this.unzipFile}`,
          targetPath: `${this.basePath}${this.targetZip}`,
          success: (res : FileManagerSuccessResult) => {
            if (this.logAble) {
              this.log += 'saveFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log('success', res)
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'saveFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: (res) => {
            this.done = true
          }
        } as UnzipFileOptions)
      },

      getSavedFileListTest: function () {
        const fileManager = uni.getFileSystemManager()
        fileManager.getSavedFileList({
          success: (res : GetSavedFileListResult) => {
            if (this.logAble) {
              this.log += 'getSavedFileListTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log("success", res)
            this.fileListSuccess = res.fileList
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'getSavedFileListTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: (res : any) => {
            console.log("complete", res)
            this.done = true
            if (res instanceof UniError) {
              this.lastCompleteError = res
            } else {
              this.fileListComplete = (res as GetSavedFileListResult).fileList
            }
          }
        } as GetSavedFileListOptions)
      },
      truncateFileTest() {
        const fileManager = uni.getFileSystemManager()
        fileManager.truncate({
          filePath: `${this.basePath}${this.writeFile}`,
          length: 7,
          success: (res : FileManagerSuccessResult) => {
            if (this.logAble) {
              this.log += 'getSavedFileListTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log("success", res)
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'getSavedFileListTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: (res : any) => {
            console.log("complete", res)
            this.done = true
            if (res instanceof UniError) {
              this.lastCompleteError = res
            }
          }
        } as TruncateFileOptions)
      },
      truncateFileSyncTest() {
        const fileManager = uni.getFileSystemManager()
        try {
          fileManager.truncateSync(
            `${this.basePath}${this.writeFile}`,
            4)
          this.done = true
        } catch (e) {
          console.log(e)
          this.done = true
        }
      },
      readCompressedFileTest() {
        const fileManager = uni.getFileSystemManager()
        fileManager.readCompressedFile({
          filePath: `${this.basePath}${this.brFile}`,
          compressionAlgorithm: "br",
          success: (res : ReadCompressedFileResult) => {
            if (this.logAble) {
              this.log += 'readCompressedFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log("success", res)
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'readCompressedFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: (res) => {
            this.done = true
          }
        } as ReadCompressedFileOptions)
      },
      readCompressedFileSyncTest() {
        console.log('readCompressedFileSyncTest')
        const fileManager = uni.getFileSystemManager()
        try {
          let data = fileManager.readCompressedFileSync(
            `${this.basePath}${this.brFile}`,
            "br")
          if (this.logAble) {
            this.log += data
          }
          this.done = true
        } catch (e) {
          if (this.logAble) {
            this.log += 'rmdirSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }

      },
      removeSavedFileTest() {
        console.log("removeSavedFileTest enter")
        const fileManager = uni.getFileSystemManager()
        fileManager.removeSavedFile({
          filePath: `${this.basePath}${this.writeFile}`,
          success: (res : FileManagerSuccessResult) => {
            if (this.logAble) {
              this.log += 'removeSavedFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            this.removeSavedFileRet = res.errMsg
            console.log("removeSavedFileTest success", res)
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'removeSavedFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('removeSavedFileTest fail', res)
          },
          complete: (res) => {
            this.done = true
          }
        } as RemoveSavedFileOptions)
      },

      statFileInfoSyncTest: function (_ : any) {
        const fileManager = uni.getFileSystemManager()
        try {
          let res = fileManager.statSync(
            // path: `${this.basePath}${this.statFile}`, //USER_DATA_PATH
            `${this.globalTempPath}${this.statFile}`, //CACHE_PATH
            this.recursiveVal)
          if (this.logAble) {
            this.log += 'statFileInfoTest success:' + JSON.stringify(res) + '\n\n'
          }
          this.statsRet = res
          this.done = true
        } catch (e) {
          if (this.logAble) {
            this.log += 'rmdirSyncTest fail:' + e + '\n\n'
          }
          this.done = true
        }

      },
      openFileTest() {
        const fileManager = uni.getFileSystemManager()
        fileManager.open({
          filePath: `${this.basePath}${this.readFile}`,
          flag: "a",
          success: (res : OpenFileSuccessResult) => {
            if (this.logAble) {
              this.log += 'openFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log("success", res)
            this.fd = res.fd
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: (res) => {
            this.done = true
          }
        } as OpenFileOptions)
      },
      openFileSyncTest() : string {
        const fileManager = uni.getFileSystemManager()
        try {
          let fd = fileManager.openSync({
            filePath: `${this.basePath}${this.readFile}`,
            flag: "r",
          } as OpenFileSyncOptions)
          if (this.logAble) {
            this.log += 'openFileSyncTest success:' + fd + '\n\n'
          }
          this.done = true
          this.fd = fd
          return fd
        } catch (e) {
          if (this.logAble) {
            this.log += 'openFileSyncTest fail:' + JSON.stringify(e) + '\n\n'
          }
          console.log('fail', e)
          this.done = true
        }
        return ""
      },
      closeSyncTest() {
        console.log('closeSyncTest')
        const fileManager = uni.getFileSystemManager()
        fileManager.open({
          filePath: `${this.basePath}${this.readFile}`,
          flag: "r+",
          success: (res : OpenFileSuccessResult) => {
            console.log("success", res)
            if (res.fd.length <= 0) {
              this.done = true
              return
            }
            try {
              console.log('closeSync')
              fileManager.closeSync({
                fd: res.fd
              } as CloseSyncOptions)
              this.log += 'closeSyncTest success:' + '\n\n'
              this.done = true
              this.closeFileRet = "close:ok"
            } catch (e) {
              if (this.logAble) {
                this.log += 'closeSyncTest fail:' + JSON.stringify(e) + '\n\n'
              }
              console.log('fail', e)
              this.done = true
            }

          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
            this.done = true
          },
          complete: (res) => {

          }
        } as OpenFileOptions)

      },
      closeTest() {
        const fileManager = uni.getFileSystemManager()
        fileManager.open({
          filePath: `${this.basePath}${this.readFile}`,
          flag: "r+",
          success: (res : OpenFileSuccessResult) => {
            console.log("success", res)
            if (res.fd.length <= 0) {
              this.done = true
              return
            }
            fileManager.close({
              fd: res.fd,
              success: (res : FileManagerSuccessResult) => {
                if (this.logAble) {
                  this.log += 'closeTest success:' + JSON.stringify(res) + '\n\n'
                }
                this.closeFileRet = res.errMsg
                console.log("success", res)
              },
              fail: (res : UniError) => {
                if (this.logAble) {
                  this.log += 'closeTest fail:' + JSON.stringify(res) + '\n\n'
                }
                console.log('fail', res)
                this.lastFailError = res
              },
              complete: (res) => {
                this.done = true
              }

            } as CloseOptions)
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
            this.done = true
          }
        } as OpenFileOptions)
      },
      writeTest() {
        const fileManager = uni.getFileSystemManager()
        fileManager.open({
          filePath: `${this.basePath}${this.readFile}`,
          flag: "r+",
          success: (res : OpenFileSuccessResult) => {
            console.log("success", res)
            if (res.fd.length <= 0) {
              this.done = true
              return
            }
            fileManager.write({
              fd: res.fd,
              data: this.writeData,
              encoding: "utf-8",
              success: (res : WriteResult) => {
                if (this.logAble) {
                  this.log += 'writeTest success:' + JSON.stringify(res) + '\n\n'
                }
                console.log("success", res)
                this.bytesWritten = res.bytesWritten
              },
              fail: (res : UniError) => {
                if (this.logAble) {
                  this.log += 'writeTest fail:' + JSON.stringify(res) + '\n\n'
                }
                console.log('fail', res)
                this.lastFailError = res
              },
              complete: (res) => {
                this.done = true
              }

            } as WriteOptions)
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
            this.done = true
          }
        } as OpenFileOptions)
      },
      writeSyncTest() {
        const fileManager = uni.getFileSystemManager()
        fileManager.open({
          filePath: `${this.basePath}${this.readFile}`,
          flag: "r+",
          success: (res : OpenFileSuccessResult) => {
            console.log("success", res)
            if (res.fd.length <= 0) {
              this.done = true
              return
            }
            try {
              let ret = fileManager.writeSync({
                fd: res.fd,
                data: this.writeData,
                encoding: "utf-8"
              } as WriteSyncOptions)
              if (this.logAble) {
                this.log += 'writeTest success:' + JSON.stringify(ret) + '\n\n'
              }
              console.log("success", ret)
              this.done = true
              this.bytesWritten = ret.bytesWritten
            } catch (e) {
              if (this.logAble) {
                this.log += 'writeTest fail:' + JSON.stringify(e) + '\n\n'
              }
              console.log('fail', e)
              this.done = true
            }
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'openFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
            this.done = true
          }
        } as OpenFileOptions)

      },
      fstatTest() {
        const fileManager = uni.getFileSystemManager()
        fileManager.fstat({
          fd: this.openFileSyncTest(),
          success: (res : FStatSuccessResult) => {
            if (this.logAble) {
              this.log += 'fstatTest success:' + JSON.stringify(res) + '\n\n'
            }
            console.log("success", res)
            this.fstat = res.stats
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'fstatTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: (res) => {
            this.done = true
          }

        } as FStatOptions)
      },
      fstatSyncTest() {
        const fileManager = uni.getFileSystemManager()
        try {
          let stat =
            fileManager.fstatSync({
              fd: this.openFileSyncTest(),
            } as FStatSyncOptions)
          if (this.logAble) {
            this.log += 'fstatSyncTest success:' + JSON.stringify(stat) + '\n\n'
          }
          this.done = true
          this.fstat = stat
        } catch (e) {
          if (this.logAble) {
            this.log += 'fstatSyncTest fail:' + JSON.stringify(e) + '\n\n'
          }
          this.done = true
        }
      },
      ftruncateFileTest() {
        const fileManager = uni.getFileSystemManager()
        fileManager.ftruncate({
          fd: this.openFileSyncTest(),
          length: 6,
          success: (res : FileManagerSuccessResult) => {
            if (this.logAble) {
              this.log += 'ftruncateFileTest success:' + JSON.stringify(res) + '\n\n'
            }
            this.ftruncateRet=res.errMsg
          },
          fail: (res : UniError) => {
            if (this.logAble) {
              this.log += 'ftruncateFileTest fail:' + JSON.stringify(res) + '\n\n'
            }
            console.log('fail', res)
            this.lastFailError = res
          },
          complete: (res : any) => {
            console.log("complete", res)
            this.done = true
            if (res instanceof UniError) {
              this.lastCompleteError = res
            }
          }
        } as FTruncateFileOptions)
      },
      ftruncateFileSyncTest() {
        const fileManager = uni.getFileSystemManager()
        try {
          fileManager.ftruncateSync({
            fd: this.openFileSyncTest(),
            length: 4
          } as FTruncateFileSyncOptions)
          if (this.logAble) {
            this.log += 'ftruncateFileSyncTest success:' + '\n\n'
          }
          this.done = true
           this.ftruncateRet=''
        } catch (e) {
          if (this.logAble) {
            this.log += 'ftruncateFileSyncTest fail:' + JSON.stringify(e) + '\n\n'
          }
          this.done = true
        }
      }

    },

1245
  }
1246 1247 1248 1249 1250
</script>

<style>

</style>