get-file-system-manager.uvue 9.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<template>
	<scroll-view>
		<view>
			<button type="primary"  @tap="readDirTest" class="btn-read-dir">读取文件夹</button>
			<button type="primary"  @tap="readFileTest" class="btn-read-file">读取文件</button>
			<button type="primary"  @tap="rmdirTest" class="btn-remove-dir">删除文件夹</button>
			<button type="primary"  @tap="unlinkTest" class="btn-unlink-file">删除文件</button>
			<button type="primary"  @tap="mkdirTest" class="btn-mkdir">创建文件夹</button>
			<button type="primary"  @tap="writeFileTest" class="btn-write-file">写入文件</button>
			<button type="primary"  @tap="renameFileTest" class="btn-rename-file">重命名文件</button>
			<button type="primary"  @tap="copyFileTest" class="btn-copy-file">复制文件</button>
      <button type="primary"  @tap="accessFileTest" class="btn-access-file">判断文件是否存在</button>
			<button type="primary"  @tap="getFileInfoTest" class="btn-get-file-info">获取文件信息</button>
杜庆泉's avatar
杜庆泉 已提交
14
      <button type="primary"  @tap="statFileInfoTest" class="btn-stat-file">获取文件Stats </button>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
		</view>
	</scroll-view>
</template>

<script>

	export default {

		data() {
			return {
				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:"锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦",
        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',
杜庆泉's avatar
杜庆泉 已提交
44
        statFile:'a/1.txt',
45 46 47 48 49
        rmDirFile:'a',
        mkdirFile:'a',
        readFile:'a/1.txt',
        recursiveVal:true,
        done: false,
50 51
        writeFileEncoding:"utf-8",
        readFileEncoding:"utf-8",
杜庆泉's avatar
杜庆泉 已提交
52
        statsRet:[] as Array<FileStats>,
53 54 55 56 57
        /**
         * 待测试的全局环境变量
         */
        basePath: uni.env.USER_DATA_PATH,
        copyToBasePath: uni.env.USER_DATA_PATH,
58 59 60
        globalTempPath:uni.env.CACHE_PATH,
        globalRootPath:uni.env.SANDBOX_PATH,
        globalInnerRootPath:uni.env.ANDROID_INTERNAL_SANDBOX_PATH,
61 62 63 64 65 66 67 68 69 70
        globalAppResourcePath:uni.env.APP_RESOURCE_PATH,
        globalUserDataPath:uni.env.USER_DATA_PATH

			}
		},
    onLoad() {
    },

		methods: {

杜庆泉's avatar
杜庆泉 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      statFileInfoTest:function(e:any){
				let fileManager = uni.getFileSystemManager()

				fileManager.stat({
					path:`${this.basePath}${this.statFile}`,
          recursive:this.recursiveVal,
					success:function(res:StatSuccessResult){
						console.log('success',res)
            this.statsRet = res.stats
            console.log('this.statsRet',this.statsRet)
					},
					fail:function(res:any){
						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 StatOptions)
			},
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

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

				fileManager.getFileInfo({
					filePath:`${this.basePath}${this.getFileInfoFile}`,
					digestAlgorithm:this.getFileInfoAlgorithm,
					success:function(res:GetFileInfoSuccessResult){
						console.log('success',res)
            this.getFileInfoSize = res.size
            this.getFileInfoDigest = res.digest
					},
					fail:function(res:any){
						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 GetFileInfoOptions)
			},


			copyFileTest:function(e:any){

				let fileManager = uni.getFileSystemManager()

				fileManager.copyFile({
					srcPath:`${this.basePath}${this.copyFromFile}`,
					destPath:`${this.copyToBasePath}${this.copyToFile}`,
					success:function(res:FileManagerSuccessResult){
						console.log('success',res)
					},
					fail:function(res:any){
						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)
			},

			renameFileTest:function(e:any){

				let fileManager = uni.getFileSystemManager()

				fileManager.rename({
					oldPath:`${this.basePath}${this.renameFromFile}`,
					newPath:`${this.basePath}${this.renameToFile}`,
					success:function(res:FileManagerSuccessResult){
						console.log('success',res)
					},
					fail:function(res:any){
						console.log('fail',res)
            this.lastFailError = res
					},
					complete:function(res:any){
            this.done = true
						console.log("complete",res)
            if(res instanceof UniError){
              this.lastCompleteError = res
            }
					}
				} as RenameOptions)
			},

			readDirTest:function(e:any){
				let fileManager = uni.getFileSystemManager()
				fileManager.readdir({
					dirPath:`${this.basePath}${this.readDir}`,
					success:function(res:ReadDirSuccessResult){
            console.log("success",res)
            this.fileListSuccess = res.files
					},
					fail:function(res:any){
						console.log('fail',res)
            this.lastFailError = res
					},
					complete:function(res:any){
            console.log("complete",res)
            this.done = true
            if(res instanceof ReadDirSuccessResult){
              this.fileListComplete = res.files
            }
            if(res instanceof UniError){
              this.lastCompleteError = res
            }
					}
				} as ReadDirOptions)
			},


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

				fileManager.writeFile({
					filePath:`${this.basePath}${this.writeFile}`,
					data:this.writeFileContent,
202
          encoding:this.writeFileEncoding,
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
					success:function(res){
						console.log('success',res)
					},
					fail:function(res){
						console.log('fail')
            this.lastFailError = res
					},
					complete:function(res){
            this.done = true
						console.log("complete")
            if(res instanceof UniError){
              this.lastCompleteError = res
            }
					}
				} as WriteFileOptions)

			},


			readFileTest:function(e:any){

				let fileManager = uni.getFileSystemManager()

				fileManager.readFile({
					filePath:`${this.basePath}${this.readFile}`,
228
					encoding:this.readFileEncoding,
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
					success:function(res:ReadFileSuccessResult){
						console.log('success',res)
            this.readFileRet = res.data
					},
					fail:function(res:any){
						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 ReadFileOptions)
			},

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

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

				let fileManager = uni.getFileSystemManager()

				fileManager.mkdir({
					dirPath:`${this.basePath}${this.mkdirFile}`,
					recursive:this.recursiveVal,
					success:function(res:FileManagerSuccessResult){
						console.log('success',res)
					},
					fail:function(res:any){
						console.log('fail',res)
            this.lastFailError = res
					},
					complete:function(res:any){
            if(res instanceof UniError){
              this.lastCompleteError = res
            }
            this.done = true
						console.log("complete",res)
					}
				} as MkDirOptions)

			},
      accessFileTest:function(e:any){
        this.accessFileRet = ''
				let fileManager = uni.getFileSystemManager()

				fileManager.access({
					path:`${this.basePath}${this.accessFile}`,
					success:function(res:FileManagerSuccessResult){
						console.log('success',res)
            this.accessFileRet = res.errMsg
					},
					fail:function(res:UniError){
						console.log('fail',res)
            this.lastFailError = res
					},
					complete:function(res:any){
            if(res instanceof UniError){
              this.lastCompleteError = res
            }
						console.log("complete",res)
            this.done = true
					}
				} as AccessOptions)

			},
			unlinkTest:function(e:any){

				let fileManager = uni.getFileSystemManager()

				fileManager.unlink({
					filePath:`${this.basePath}${this.unlinkFile}`,
					success:function(res:FileManagerSuccessResult){
						console.log('success',res)
					},
					fail:function(res:UniError){
						console.log('fail',res)
            this.lastFailError = res
					},
					complete:function(res:any){
            if(res instanceof UniError){
              this.lastCompleteError = res
            }
						console.log("complete",res)
            this.done = true
					}
				} as UnLinkOptions)
			},

		}
	}
</script>

<style>


</style>