uniapp 读,写,删本地文件

<template>

<view>

<view class="p-2">

<view class="bg-primary p-2 rounded flex align-center justify-center" style="color: #fff;" hover-class="bg-hover-primary" @click="dataset">写入{{count}} s</view>

</view>

<view class="p-2">

<view class="bg-primary p-2 rounded flex align-center justify-center" style="color: #fff;" hover-class="bg-hover-primary" @click="read">读取</view>

</view>

<view class="p-2">

<view class="bg-primary p-2 rounded flex align-center justify-center" style="color: #fff;" hover-class="bg-hover-primary" @click="deletes">删除</view>

</view>

</view>

</template>

<script>

import { mapState } from 'vuex'

export default {

data() {

return {

count: '',

timer: null,

resultdata:[]

}

},

computed: {

...mapState({

token:state=>{

return state.user ? state.user.token : null

},

user:state=>{

return state.user ? state.user : null

}

}),

},

methods: {

read(){

//console.log("读取")

plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, (fs)=> {

// fs.root是根目录操作对象DirectoryEntry

let a = fs.root.toURL() //通过此方法我们可获取存储在app私用环境下的相对跟路径。

console.log(a + '/box/' + 'test.json')

fs.root.getFile(a + '/box/' + 'test.json', {

create: false

}, (fileEntry)=> {

// 文件在手机中的路径

// console.log(fileEntry.fullPath)

fileEntry.file((file)=> {

var fileReader = new plus.io.FileReader();

fileReader.readAsText(file, 'utf-8');

fileReader.onloadend =(evt)=> {

var news = [];

var result = evt.target.result;

console.log(result)

if (result != ""){

this.resultdata=JSON.parse(result)

}

}

});

});

})

},

wetir(){

//console.log("写入")

plus.io.requestFileSystem(

plus.io.PUBLIC_DOCUMENTS, // 文件系统中的根目录

fs => {

let a = fs.root.toURL() //通过此方法我们可获取存储在app私用环境下的相对跟路径。

// 创建或打开文件, fs.root是根目录操作对象,直接fs表示当前操作对象

fs.root.getFile(a + '/box/' + 'test.json', {

create: true // 文件不存在则创建

}, fileEntry => {

// 文件在手机中的路径

// console.log(fileEntry.fullPath)

fileEntry.createWriter(writer => {

// 写入文件成功完成的回调函数

writer.onwrite = e => {

console.log("写入数据成功");

};

//console.log(this.resultdata)

// 写入数据

writer.write(JSON.stringify(this.resultdata));

uni.hideLoading()

})

}, e => {

console.log("getFile failed: " + e.message);

});

},

e => {

console.log(e.message);

}

);

},

deletes(){

//console.log("删除文件")

plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, (fs)=> {

// fs.root是根目录操作对象DirectoryEntry

let a = fs.root.toURL();

//删除目录不能指向文件名 只能指向文件夹

console.log(a+'/box/')

plus.io.resolveLocalFileSystemURL(a+'/box/' ,(entry)=> {

//读取这个目录对象

var directoryReader = entry.createReader();

//读取这个目录下的所有文件

directoryReader.readEntries((entries)=> {

console.log(entries.length)

//如果有才操作

console.log(entries, 'entries')

if (entries.length > 0) {

//删除目录将会删除其下的所有文件及子目录 不能删除根目录,如果操作删除根目录

//将会删除目录下的文件及子目录,不会删除根目录自身。 删除目录成功通过succesCB

//回调返回,失败则通过errorCB返回。

entry.removeRecursively((entry)=> {

console.log('删除成功回调')

//删除成功回调

this.resultdata=[]

}, (e) =>{

//错误信息

console.log(e.message + ' ?!')

})

}

})

})

})

},

getCode(){

const TIME_COUNT = 10; //限定时间

if (!this.timer) {

this.count = TIME_COUNT;

this.timer = setInterval(() => {

if (this.count > 0 && this.count <= TIME_COUNT) {

this.count--; //全局的数据改变 倒计时

} else {

clearInterval(this.timer);

this.timer = null; //清除定时器

//需要执行的代码

return this.$H.get(`/url`,{

token:true

}).then(res=>{

this.resultdata=res

console.log(res)

this.wetir()

}).catch(err=>{

uni.hideLoading()

})

}

}, 1000)

}

} ,

dataset(){

//避免用户重复点击

this.getCode()

uni.showLoading({

title: '同步中...'

});

},

}

}

</script>

相关推荐
写代码的皮筏艇11 分钟前
replace方法
前端·javascript
C澒11 分钟前
AI 生码 - API2CODE:接口智能匹配与 API 自动化生码实践
前端·低代码·ai编程
idcu16 分钟前
Lyt.js AI:让前端开发进入智能生成时代
前端
idcu17 分钟前
深入 Lyt.js 编译器:.lyt 文件如何增强 HTML 模板能力
前端
即答侠17 分钟前
实时 AI copilot 的 4 级 fallback 设计:用户感知 0 中断,SLA 从 92% 拉到 99.6%
前端·人工智能
无心使然19 分钟前
Openlayers调用ArcGis地图服务之五 —— 要素识别(/identify)
前端·vue.js·数据可视化
Dxy123931021625 分钟前
HTML 如何设置 Div 阴影悬浮边框:从基础到进阶
前端·html·css3
好运的阿财26 分钟前
OpenClaw工具拆解之browser+agents_list
前端·人工智能·机器学习·开源软件·ai编程·openclaw·openclaw工具
JarvanMo33 分钟前
八个开源Flutter应用,让你成为更好的开发者
前端
ZC跨境爬虫39 分钟前
Apple官网复刻第二阶段day_2:(前端模块化还原苹果官网WATCH海报)
前端·ui·重构·html·状态模式