Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native : How to create text file and save it to external storage

Tags:

react-native

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.

like image 518
pradip_android Avatar asked Dec 10 '18 07:12

pradip_android


People also ask

How do I download a text file from react?

To download a string as . txt file in React, we can convert the string into a blob. And then we can create a hidden link that has the blob's object URL set as the href attribute and click on the link programmatically to download it.


1 Answers

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

Install :

npm i react-native-fs

Creating File Example :

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);
  });

Thank you..!

~ Praz

like image 151
Praz Solver Avatar answered Jan 04 '23 12:01

Praz Solver