itinance / react-native-fs

Native filesystem access for react-native
MIT License
4.97k stars 986 forks source link

Asset file download to device #1204

Open Shivani12345 opened 1 year ago

Shivani12345 commented 1 year ago

I need to download file which is in application to iOS device. which is located like

- appfolder 
   -src
      -assets
          -icons
              -bootsplash.png

I implemented code like below but the source path is not found by copyFile() function

sourcepath='src/assets/icons/bootsplash.png';
const destinationPath = `${RNFS.DocumentDirectoryPath}/bootsplash.png`;
RNFS.copyFile(sourcepath, destinationPath, 10, 10)
      .then(() => {
        console.log('Asset copied successfully to:', destinationPath);
      })
      .catch(error => {
        console.error('Error copying asset:', error);
      });

When I try sourcepath with 'Users/m*****/Documents/reactProjs/MyApplication/src/assets/icons/bootsplash.png' its working but I want to set this path dynamically. How can we get application path dynamically with code or any other solution?

VMBindraban commented 1 year ago

The sourcepath is not correct, it must be an absolute path. See constants.

It should be something like: ${RNFS.MainBundlePath}/src/assets/icons/bootsplash.png (depends on the OS)

Shivani12345 commented 1 year ago

@VMBindraban I have put the same path and the path like

/Users/SAN***/Library/Developer/CoreSimulator/Devices/6FBB8EF2-3F3E-4E17-9525-1A051ETYTRU/data/Containers/Bundle/Application/B24AF1CD-480B-4374-B494-CD5240WSD123/BaseCodeStructure.app/src/assets/icons/bootsplash.png

ERROR : [Error: The file “bootsplash.png” couldn’t be opened because there is no such file.]

MY CODE is

RNFS.copyFile( ${RNFS.MainBundlePath}/src/assets/icons/bootsplash.png, destinationPath, ) .then(() => { console.log('Asset copied successfully to:', destinationPath); }) .catch(error => { console.error('Error copying asset:', error); });

VMBindraban commented 1 year ago

Can you try DocumentDirectoryPath instead of MainBundlePath?