- 安装
#安装
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: (<