React Native:如何创建文本文件并将其保存到外部存储 [英] React Native : How to create text file and save it to external storage

查看:74
本文介绍了React Native:如何创建文本文件并将其保存到外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 React Native 为 Android 和 iOS 设备创建文本文件.我们如何将字符串数据存储在文本文件中并将其保存到设备的外部存储中.请建议是否有相同的图书馆.

I am trying to create a text file using react native for both Android and iOS device. How can we store the string data in text file and and save it to external storage of device. Please suggest if any library for the same.

推荐答案

使用 npm 包 react-native-fs for it..!

Use npm package react-native-fs for it..!

安装:

npm i react-native-fs

创建文件示例:

var RNFS = require('react-native-fs');

var path = RNFS.DocumentDirectoryPath + '/test.txt';

// write the file
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
  .then((success) => {
    console.log('FILE WRITTEN!');
  })
  .catch((err) => {
    console.log(err.message);
  });

谢谢...!

~普拉兹

这篇关于React Native:如何创建文本文件并将其保存到外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆