Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move file from tmp to documents using react-native-fs

Tags:

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);
    });
}
like image 626
liver Avatar asked Jun 05 '17 21:06

liver


1 Answers

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)

like image 147
liver Avatar answered Oct 14 '22 03:10

liver