Aravind Reddy
Aravind Reddy

Reputation: 363

Using React Native, how do you save the responseJson of a fetch into a file in front end

I am currently trying to save the results of a fetch call into a file(.json or .csv if possible). My code is as follows:

  fetch('https://xxxxxxxxxxx.execute-api.us-east-1.amazonaws.com/prod/export', {
          method: 'post',
          headers:{
            'Accept': 'application/json',
            'Content-type': 'application/json'
          },
          body:JSON.stringify({
            deviceid: item,
            fromtime: myEpoch1.toString(),
            totime: myEpoch2.toString()
          })
        })
        .then((response) => response.json())
          .then((responseJson) => {
            console.log(responseJson); //this result to a file
          })

Thanks a ton

Upvotes: 0

Views: 133

Answers (1)

Tushar Pandey
Tushar Pandey

Reputation: 4857

you ca use react-native-fs

here I have pasted code shared by this npm-package.

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

// create a path you want to write to
// :warning: on iOS, you cannot write into `RNFS.MainBundlePath`,
// but `RNFS.DocumentDirectoryPath` exists on both platforms and is writable
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);
  });

Upvotes: 1

Related Questions