6

I'm trying to move a file selected from a document picker to the Document Directory using react-native-fs and react-native-document picker.

However, I get the error below:

Error: “file name.mp3” couldn’t be moved to “Documents” because either the former doesn't exist, or the folder containing the latter doesn't exist.

What am I doing wrong?

FYI, I'm using iOS.

openDocumentPicker() {
  DocumentPicker.show({
    filetype: ['public.audio'],
  },(error,url) => {
    console.log(url);
    this.saveAudio(url);
  });

}

saveAudio(url) {

  var destPath = RNFS.DocumentDirectoryPath + '/' + 'name';

  RNFS.moveFile(url, destPath)
    .then((success) => {
      console.log('file moved!');
    })
    .catch((err) => {
      console.log("Error: " + err.message);
    });
}

2 Answers 2

2

I believe I've found the error. The issue was that the file I was uploading had a space in it. I needed to decode the URL first before uploading, like so:

var decodedURL = decodeURIComponent(url)

Then I could move the file over.

RNFS.copyFile(decodedURL, destPath)

Sign up to request clarification or add additional context in comments.

1 Comment

What I discovered, interesting fact. Android OS's still having problem when the name contains percent symbol, %. So I need to initially decode uri and replace each % symbol to %25. But this solution breaks it on iOS devices. So I'm using decodeURIComponent(uri.replace(/%/g, '%25')) for android and just decodeURIComponent(uri) for iOS.
2

It happened to me when destination folder doesn't exist.

[tid:com.facebook.react.JavaScript] 'error!', { [Error: The file “source.jpg” doesn’t exist.]

It's a wrong error message from react-native-fs. It should tell target path folder not exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.