React Native集成react-native-fs(本地文件系统)

本文介绍如何在React Native项目中集成并使用react-native-fs库,实现本地文件系统的操作。从安装库开始,到创建工具类,详细阐述了调用过程。
摘要由CSDN通过智能技术生成
  • 安装
#安装
npm install --save react-native-fs

#链接依赖
react-native link react-native-fs
  • 工具类(可直接调用)
/**
 * FileName: RNFSUtils.js
 * Author: hf
 * Date: 2019/2/11 14:39
 * Description:封装对文件的【下载、文本写入、文本读取、文本追加、删除】的工具类方法
 *
 */
import RNFS from 'react-native-fs';


/** @namespace RNFS.ExternalDirectoryPath */
/**
 * 常用文件存储目录(ios与android)
 *
 * RNFS.MainBundlePath
 * RNFS.CachesDirectoryPath
 * RNFS.DocumentDirectoryPath
 * RNFS.TemporaryDirectoryPath
 * RNFS.LibraryDirectoryPath
 * RNFS.ExternalDirectoryPath
 * RNFS.ExternalStorageDirectoryPath

 */
const ExternalDirectoryPath = RNFS.ExternalDirectoryPath;


/**
 * 功能描述: <br>
 * 〈文件下载(图片、文件、音频、视频)〉
 *
 * @MethodName: _downloadFile
 * @Author: hf
 * @Version: 1.0.0
 * @Date: 2019/2/11 14:46
 * @Param: [formUrl 要下载的文件地址, targetName 目标文件名称(类似text.txt),callback: 1:下载成功   0:下载失败]
 *
 */
export const _downloadFile = (formUrl, targetName) => {
   
    // 获取下载文件本地保存路径
    const toLoadPath = `${
     ExternalDirectoryPath}/${
     targetName}`;
    RNFS.downloadFile({
   
        fromUrl: formUrl,
        toFile: toLoadPath,
        progressDivider: 5,
        begin: (<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值