alpha0010 / react-native-file-access

Filesystem access for React Native
MIT License
298 stars 18 forks source link

Saving to downloads directory #37

Closed ShivamJoker closed 2 years ago

ShivamJoker commented 2 years ago

Hello thanks for your great work Can we also save the files in download directory ?

ShivamJoker commented 2 years ago

I am trying using the external cp but getting

Error: Failed to copy to 'downloads'
alpha0010 commented 2 years ago

Assuming Android, have you tried requesting WRITE_EXTERNAL_STORAGE https://reactnative.dev/docs/permissionsandroid ?

ShivamJoker commented 2 years ago

Okay even after adding permission that it is not working

 LOG  file:///data/user/0/com.introverttest/cache/5fc1e891-2b3c-4dab-b85f-dc290b3d6cbdimagemarker.jpg
 LOG  [Error: Failed to copy to 'downloads' ('Introvert Certificate.jpg')]
  const handleDownload = async () => {
    try {
      console.log(imgUri);

      await checkStoragePermission();
      const url = await FileSystem.cpExternal(
        imgUri,
        'downloads',
        'Introvert Certificate.jpg',
      );
      console.log('copied', url);
    } catch (err) {
      console.log(err);
    }
  };
alpha0010 commented 2 years ago

Looks like arguments are reversed. Try:

await FileSystem.cpExternal(
  imgUri,
  'Introvert Certificate.jpg',
  'downloads',
);

(Related, if you decide to switch to using TypeScript at some point, this library includes definitions that should give a warning for this type of situation.)

ShivamJoker commented 2 years ago

Damn 🥺 yeah I generally use typescript in my projects but this was for someone else.

Thanks for replying