当前位置: 首页 > news >正文

uniapp iosApp H5+本地文件操作(写入修改删除等)

h5+ 地址 html5plus

以csv文件为例,写入读取保存修改删除文件内容,传输文件等

1.save 文件保存

function saveCsv(data,pathP,path){// #ifdef APP-PLUSreturn new Promise((resolve, reject) => {plus.io.requestFileSystem( plus.io.PUBLIC_DOCUMENTS, function( fs ) {// 通过fs.root获取DirectoryEntry对象进行操作fs.root.getDirectory(pathP,{create:true},direEntry =>{plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, fs => {fs.root.getFile(path, {create: true}, fileEntry => {fileEntry.createWriter(writer => {writer.onwrite = e => {resolve(`file://${e.target.fileName}`);// uni.showModal({// 	title: '提示',// 	content: `导出成功,文件路径为${e.target.fileName.replace('/storage/emulated/0', '')}`,// 	cancelText: '了解并关闭',// 	confirmText: '预览文件',// 	success: result => {// 		if (result.confirm) {// 			uni.openDocument({// 				filePath: `file://${e.target.fileName}`,// 			})// 		}// 	}// });}writer.write(data);}, writerErr => {resolve(500)uni.showToast({title: '导出文件失败,请检查是否有权限',icon: 'none'});})})})})}, fsErr => {uni.showToast({title: '导出文件失败,请检查是否有权限',icon: 'none'});})})// #endif	
}// #ifdef APP-PLUS
let submitCsv = savedata.map(e=> JSON.stringify(e)).join("\n")saveCsv(submitCsv,`${user}/triallist/${_this.trialId}`,`${user}/triallist/${_this.trialId}/scoresubmit.csv`).then(path=>{console.log(path)
})
// #endif

2. read 读取

function readListCsv(path){let reader = null;let data=[];// #ifdef APP-PLUSreturn new Promise((resolve, reject) => {plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS,fs => {fs.root.getFile(path,{create:true},fileEntry=> {fileEntry.file( function ( file ) {reader = new plus.io.FileReader();reader.onloadend = function ( e ) {if(e.target.result){//返回的data处理取决于存入时的操作,不同格式不同操作data = e.target.result.split('\n').map(e=> JSON.parse(e))resolve(data);}else{data = []resolve(data);}};reader.readAsText( file );}, function ( e ) {uni.showToast({title: e.message,icon: 'none'});});})})})// #endif
}// #ifdef APP-PLUS
//自定义文件路径
readListCsv(`${user}/triallist/${this.trialId}/time.csv`).then(res=>{console.log(res)
})

3. delete 删除目录

//已存在目录,删除重建
let path = uni.getStorageSync('path') + `/${user}/triallist/${_this.trialId}`;
plus.io.resolveLocalFileSystemURL(path, function( entry ) {let directoryReader = entry.createReader(); //获取读取目录对象directoryReader.readEntries( function( entries ){console.log(entries.length);if (entries.length > 0) {entry.removeRecursively(function(entry) {callback();//删除成功回调---获取不到。。。。    }, function(e) {//错误信息    console.log(e.message + ' ?!')})}})
})

4.upload 上传(uniapp)循环上传目录内文件

// #ifdef APP-PLUS
//使用uniapp上传需要上传文件获取uniapp生成的路径后,调用上传接口
updateResume(){let path = uni.getStorageSync('path') + `/${user}/triallist/${this.trialId}/......`plus.io.resolveLocalFileSystemURL(path, function( entry ) {var directoryReader = entry.createReader(); //获取读取目录对象directoryReader.readEntries( function( entries ){for(let i=0; i < entries.length; i++ ){uni.saveFile({tempFilePath: path + entries[i].name,success: function (res) {uni.uploadFile({url:NEV.BASE_URL +'/api/api..../add',filePath:res.savedFilePath,header: {'X-Access-Token':uni.getStorageSync('token'),'Authorization': 'Bearer ' + uni.getStorageSync('token'),"Content-Type": "multipart/form-data",},name:'MultipartFile',formData: {  //接口携带的参数trialId:_this.trialId,},success() {console.log('一个文件上传成功');},fail() {console.log('一个文件上传失败');}})},fail() {console.log('一个文件上传失败');}});console.log( entries[i].name );}}, function ( e ) {alert( "Read entries failed: " + e.message );} );});// #endif
}
http://www.lryc.cn/news/186582.html

相关文章:

  • 蓝桥杯 字符串和日期
  • Vue13 监视属性
  • 会员商城小程序的作用是什么
  • 排序算法——希尔排序
  • SpringBoot项目整合MybatisPlus持久层框架+Druid数据库连接池
  • 导致 JVM 内存泄露的 ThreadLocal 详解
  • 使用预约小程序app有什么方便之处
  • 【转】ubuntu 安装 OpenCv 4.6脚本 installOCV.sh
  • Android 视图动画与属性动画的区别
  • Springboot——jxls实现同sheet多个列表展示
  • 分布式软件架构——服务端缓存的三种属性
  • Flink之Watermark策略代码模板
  • ubuntu 安装postgresql,增加VECTOR向量数据库插件 踏坑详细流程
  • 基于Springboot实现影视影院订票选座管理系统【项目源码+论文说明】分享
  • mysql批量插入数据,跳过唯一索引报错
  • 论文阅读--Energy efficiency in heterogeneous wireless access networks
  • Redis的C客户端(hiredis库)使用
  • 光引擎、光模块、光器件之间的关系和区别
  • 【办公-excel】两个时间相减 (二) - 带毫秒的时间进行相减操作
  • 二次封装View Design的table组件,实现宽度自适应,内容在一行展示
  • Node.js代码漏洞扫描工具介绍——npm audit
  • node.js知识系列(3)-每天了解一点
  • Zabbix监控系统 自定义监控项、自动发现与自动注册
  • Python信号之分享
  • 环信web、uniapp、微信小程序SDK报错详解---登录篇
  • DAZ To UMA⭐五.模型在Blender中的配置教程
  • 网络安全工具汇总
  • day-65 代码随想录算法训练营(19)图论 part 04
  • C++ - 完美语义(右值引用的中篇) - lambda表达式
  • 常见排序算法详解