react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

ENOENT: no such file or directory, open

Open florinlazau opened this issue 5 years ago • 3 comments

Hello,

I'm using react-native-document-picker in order to pick an audio file.

After this i'm using RNFS.readFile(nft.teaser.url, 'base64') to obtain a base64 that should be upload in a blob.

The problem is that RNFS.readFile throws an exception:

Error: ENOENT: no such file or directory, open '/Users/xxx/Library/Developer/CoreSimulator/Devices/F21F02D1-2AEE-4DDE-A5DA-B8EEE28073FB/data/Containers/Data/Application/C33F0FAE-C0F5-429A-9CCB-FF7A5E586381/tmp/yyyy-Inbox/file_example_MP3_700KB%20(1).mp3'
    at fn (NativeModules.js:99)
    at readFileGeneric (FS.common.js:160)
    at Object.readFile (FS.common.js:310)
    at _callee4$ (VM13 actions.bundle:194)
    at tryCatch (runtime.js:63)
    at Generator.invoke [as _invoke] (runtime.js:293)
    at Generator.next (runtime.js:118)
    at tryCatch (runtime.js:63)
    at invoke (runtime.js:154)
    at runtime.js:189

It's strange because the path I pass to readFile begins with "files//" and in exception looks like this part was removed from the path.

Any help would be appreciated.

Thanks!

florinlazau avatar Oct 23 '20 14:10 florinlazau

Hi @florinlazau ! It seems that you are using an iOS platform. Because your files contain some whiteSpace will convert to '%20' by react-native-document-picker, we need to do decodeURI to let it work. Maybe you can try to use the code below to let it work

const split = res.fileCopyUri.split('/');
const name = split.pop();
const inbox = split.pop();
const realPath = `file://${
  RNFS.TemporaryDirectoryPath
  }/${inbox}/${decodeURI(name)}`;
console.log(realPath);

hank121314 avatar Nov 03 '20 02:11 hank121314

@hank121314 Thanks for your answer. I'm not sure what it looked like when you posted it, but now you need to remove one slash as RNFS.TemporaryDirectoryPath has it at the end:

    const uriParts = uri.split('/');
    const name = uriParts.pop();
    const inbox = uriParts.pop();
    const realPath = `file://${RNFS.TemporaryDirectoryPath}${inbox}/${decodeURIComponent(name)}`;

Trafiz avatar Jul 28 '21 12:07 Trafiz

Wrapping the uri with decodeURIComponent should do the trick :)

 RNFS.readFile(decodeURIComponent(uri))

ADCrouch avatar Aug 21 '22 00:08 ADCrouch