12.React Native文件创建、查看、下载、上传-react-native-fs

目录

1.react-native-fs是什么?

2.react-native-fs支持哪些功能?

3.react-native-fs如何使用?

4.react-native-fs功能介绍?

5.文件操作说明

5.1文件目录说明

5.2文件创建

5.3文件删除

5.4文件读取

5.5文件上传(支持IOS和Android)

5.6文件修改

5.7文件移动和复制

5.8文件是否存在

5.9创建文件夹

5.10文件下载


在移动端实际开发中难免会涉及涉及缓存:
a.AsyncStorage:一些简单文本、用户身份Token或者JSON文件等可以用AsyncStorage,类似h5的数据存储LocalStorage;在 iOS 上,AsyncStorage在原生端的实现是把较小值存放在序列化的字典中,而把较大值写入单独的文件。在 Android 上,AsyncStorage会尝试使用RocksDB,或退而选择 SQLite。

可参考:

https://blog.csdn.net/ahou2468/article/details/88954215

b.Realm:Realm不是ORM(对象关系映射数据库),也不是建立在sqlite之上。相反,我们为移动应用程序开发人员构建了一个完整的数据库,它使用动态映射到完整的自定义数据库引擎(而不仅仅是一个键值存储)的本机JavaScript对象。这允许我们在保持性能的同时提供一个简单的API。使用Realm,您可以建模复杂的数据,链接图形中的对象,以及组成高级查询。

可参考:
 https://blog.csdn.net/ahou2468/article/details/94384048

c.文件存储(react-native-fs):

实现了react native的访问本地文件系统

主要介绍一下本地文件存储(react-native-fs);

注意事项:

对于rn<0.57和/或gradle<3,您必须安装react native fs的版本2.11.17!
对于rn>=0.57和/或gradle>=3,您必须时安装react native fs的版本2.13.2!

1.react-native-fs是什么?

实现了react native的访问本地文件系统;

2.react-native-fs支持哪些功能?

支持文件的创建、删除、查看、上传、下载;

3.react-native-fs如何使用?

a.安装(install)

npm install react-native-fs --save

b.链接(link)

react-native link react-native-fs

执行结果:

rnpm-install info Linking react-native-fs ios dependency 
rnpm-install info Platform 'ios' module react-native-fs has been successfully linked 
rnpm-install info Linking react-native-fs android dependency 
rnpm-install info Platform 'android' module react-native-fs has been successfully linked 

如果link失败,或者link之后仍旧不能import,则考虑手动link,详情查看官网教程,传送门

4.react-native-fs功能介绍?

文件创建,修改,读取,删除,上传,下载 ;

5.文件操作说明

5.1文件目录说明

参数说明:

import RNFS from 'react-native-fs'

MainBundlePath (String) 主bundle目录的绝对路径; (在Android上不适合)
CachesDirectoryPath (String) 缓存目录的绝对路径;
ExternalCachesDirectoryPath (String) 外部缓存目录的绝对路径 (仅在Android上支持)
DocumentDirectoryPath (String) 文档目录的绝对路径
TemporaryDirectoryPath (String) 临时目录的绝对路径(返回到Android上的缓存目录)
LibraryDirectoryPath (String) NSLibraryDirectory绝对路径; (在iOS上支持)
ExternalDirectoryPath (String) 外部文件的绝对路径,共享目录(在android上支持)
ExternalStorageDirectoryPath (String) 外部存储的绝对路径,共享目录 (在android上支持)

Android打印日志:

MainBundlePath: undefined
CachesDirectoryPath: /data/user/0/com.helloworld/cache
ExternalCachesDirectoryPath: /storage/emulated/0/Android/data/com.helloworld/cache
DocumentDirectoryPath: /data/user/0/com.helloworld/files
ExternalDirectoryPath: /storage/emulated/0/Android/data/com.helloworld/files
ExternalStorageDirectoryPath: /storage/emulated/0
TemporaryDirectoryPath: /data/user/0/com.helloworld/cache
LibraryDirectoryPath: undefined
PicturesDirectoryPath: /storage/emulated/0/Pictures
FileProtectionKeys: undefined

android使用注意事项:

使用ExternalStorageDirectoryPath时,需要请求权限(在Android上)才能读取和写入外部存储;在Android23版本以后需要申请权限;申请权限示例:https://facebook.github.io/react-native/docs/permissionsandroid

 

IOS的打印日志

MainBundlePath: /Users/fandong/Library/Developer/CoreSimulator/Devices/2186A64F-
70D4-4BF2-A2F8-AE357F828D33/data/Containers/Bundle/Application/88D43269-12E3-4720-80D1-5618EBEF75DE/HelloWorld.app
CachesDirectoryPath: /Users/fandong/Library/Developer/CoreSimulator/Devices/2186A64F-70D4-4BF2-A2F8-
AE357F828D33/data/Containers/Data/Applicatio
  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值