0

After writing data to a file using react-native-fs where does the file go?

I am able to write the file and view it when i am testing in my local machine using ExternalStorageDirectoryPath in android But when i push my app to play store I am unable to perform the write operation. Same thing is happening while using DocumentDirectoryPath in IOS

I can find the file in emulator and external device when i run react-native-run-android but not when i published the app.

Reference: Saving a files in React-Native using reart-native-fs In this post only files are shown from emulators and i am looking for a solution to find the file in real world devices after installing my app. Thanks

Below is my code

fetch('https://xxxxxxxxxxxx.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.setState({exportButton:false});
            if(responseJson){
              var path = RNFS.ExternalStorageDirectoryPath + '/'+ myEpoch1 +'-to-'+ myEpoch2 +'.json';
              RNFS.writeFile(path, JSON.stringify(responseJson), 'utf8')
              .then((success) => {
                console.log('FILE WRITTEN!');
                Alert.alert(
                  'Sucesss: File saved',
                  'File downloaded and saved successfully to ' + path + ' device' ,
                  [
                    {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
                    {text: 'OK', onPress: () => console.log('OK Pressed')},
                  ],
                  { cancelable: false }
                )
              })
              .catch((err) => {
                console.log(err.message);
              });
            }
          })```
Aravind Reddy
  • 353
  • 4
  • 18
  • 1
    Things that I would check: Trailing slash for `ExternalStorageDirectoryPath` in production build, WRITE_EXTERNAL_STORAGE permission for android, Dont do response.json() if all you doing with data is JSON.stringify() – sbqq Apr 03 '20 at 15:56
  • Thanks for the comment @sebinq . I have cross verified and modified the suggestions and still i face the same. NOTE: When i run react-native-run-android with a android device connected it works (As when i open the screen i get a pop up requesting for permissions to read write data) ...But when i publish the build and install app in an android device and it doesn't work (when i open the screen it never asks for permissions ```when using ExternalStorageDirectoryPath it's necessary to request permissions (on Android) to read and write on the external storage``` I did this as well – Aravind Reddy Apr 06 '20 at 13:11

0 Answers0