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);
    });
}
liver
  • 498
  • 4
  • 21

2 Answers2

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)

liver
  • 498
  • 4
  • 21
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.

Val
  • 21,938
  • 10
  • 68
  • 86