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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With