文章详情
【微信小程序】微信小程序读取本地文件--学习微信小程序之路02
发布时间:2022-01-09
沉默小管-技术博客
微信小程序读取本地文件--学习微信小程序之路02
1172
2
原文地址:
https://www.php-china.com//detail/91
H1
H2
H3
H4
H5
H6
# 问题描述 我想读取微信小程序本地文件,但是微信小程序好像规定要在指定的文件进行读取 # 解决流程 1.把本地文件**复制并保存**在微信小程序指定的文件 2.读取文件 ```javascript //读取文件的路径 //wx.env.USER_DATA_PATH微信用户数据路径 let aFilePath = `${wx.env.USER_DATA_PATH}/data.json` let getFile = wx.getFileSystemManager() let readFileData = getFile.readFileSync(aFilePath,'utf-8') //读取为空 if(!readFileData){ //文件数据 let jsonData = `{ "data": [ { "id": 1, "name": "猪肉【黑猪】", "originalPrice": "234", "curPrice": "20", "num":10 "proDetail": { "from":"中国", "method":"山林低密度定制散养", "variety":"旧院黑鸡蛋", "specification":30, "curPrice": "20", "sales":8283 }, "addTime": "1640918029", "updateTime": "1640918029" }, { "id": 2, "name": "猪肉【红猪】", "originalPrice": "47", "curPrice": "33", "num":10, "proDetail": { "from":"非洲", "method":"山林低密度定制散养", "variety":"旧院黑鸡蛋", "specification":30, "curPrice": "33", "sales":8533 }, "addTime": "1640918029", "updateTime": "1640918029" }, { "id":3, "name": "猪肉【黄猪】", "originalPrice": "78", "curPrice": "44", "num":10, "proDetail": { "from":"美国", "method":"山林低密度定制散养", "variety":"旧院黑鸡蛋", "specification":30, "curPrice": "44", "sales":8123 }, "addTime": "1640918029", "updateTime": "1640918029" }, { "id": 4, "name": "猪肉【灰猪】", "originalPrice": "66", "curPrice": "20", "num":10, "proDetail": { "from":"中国", "method":"山林低密度定制散养", "variety":"旧院黑鸡蛋", "specification":30, "curPrice": "20", "sales":863 }, "addTime": "1640918029", "updateTime": "1640918029" } ] } `; //把需要的文件复制过去并命名 getFile.writeFileSync(aFilePath,jsonData,'utf-8') readFileData = getFile.readFileSync(aFilePath,'utf-8') } console.log(JSON.parse(readFileData)) ```
上一篇
下一篇
收藏
支持
标签:
最新文章
统计信息
联系站长
加入技术群
日志总数:92篇
网站运行:1424天
累计访客:13007
热门文章
1. PHP基础概念
2. redis:ERR AUTH <password> called without any password configured for the default user解决办法
3. PHP的语言结构之包含文件
4. PHP中public,private,protected的区别
5. PHP魔术方法总结
6. php如何转换json格式中的中文
7. PHP7新特性(常用)
8. PHP7上项目报错A non-numeric value encountered解决办法
9. 运行golang出现invalid memory address or nil pointer dereference错误
10. git常用命令集合