joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.8k stars 770 forks source link

File not exists #116

Closed rochapablo closed 5 years ago

rochapablo commented 5 years ago

Following this, i'm trying to get the content file;

Using react-native-document-picker, after select the file I do this:

if (Platform.OS === 'ios') {
    let arr = data.uri.split('/')
    const dirs = RNFetchBlob.fs.dirs
    filename = RNFetchBlob.wrap(`${dirs.DocumentDir}/${arr[arr.length - 1]}`)
} else {
    filename = data.uri
}
const file = await RNFetchBlob.fs.readFile(filename, 'base64')

I also tried:

// filename = RNFetchBlob.wrap(data.uri)
// filename = data.uri

But I always get the same error file not exists

This is the data from react-native-document-picker:

{
  "size": 16669,
  "name": "Imagem JPEG.jpeg",
  "uri": "file:///private/var/mobile/Containers/Data/Application/63F987D9-81F6-41B1-80A1-8E9E102C82FD/tmp/br.com.app-Inbox/Imagem%20JPEG.jpeg",
  "type": "image/jpeg"
}

What could I be missing?

"react": "16.0.0-alpha.12"
"react-native": "^0.48.4"
"rn-fetch-blob": "^0.10.11"
"react-native-document-picker": "git://github.com/Elyx0/react-native-document-picker.git#v3",
ihavenoface5 commented 5 years ago

Is the %20 in the filename intentional (Imagem%20JPEG.jpeg)? I think iOS would have a problem with this.

rochapablo commented 5 years ago

I thought about that and rename the file, but it still cant find the file.

dantman commented 5 years ago

Try using v3 and the fetch() built into React Native. The React Native FS and React Native Fetch Blob libraries are not supported.

rochapablo commented 5 years ago

Wait!!!

Just using filename = data.uri.replace('file:', '') as @andonivianez said here, did work!

The final code:

if (Platform.OS === 'ios') {
      filename = data.uri.replace('file:', '')
} else {
      filename = data.uri
}

I'm going grab a beer, closing for now!

MikaelStenstrand commented 5 years ago

Thanks @rochapablo for the tip! After getting the URI right with your suggested method, I had to still add the buffer.

yarn add buffer and importing the buffer to the project global.Buffer = global.Buffer || require("buffer").Buffer; // buffer needed for file reading Then everything worked for me.

karltaylor commented 5 years ago

I still don't know why this is the correct way to read IOS urls in the docs. Took me ages.

BatDroid commented 5 years ago

based on this for android i did this :

uri.split('raw%3A')[1].replace(/\%2F/gm, '/')

ios:

uri.replace("file://", "")
mishuagopian commented 3 years ago

Hey @rochapablo why did you add Buffer in order to work?

SandeepKharbanda commented 2 years ago

I have tried it with the below way and After that, It's working fine.

  let documentURI = doc.uri;
  if (Platform.OS === 'ios' ) {
    documentURI = decodeURIComponent(documentURI.replace('file://', ''));
  }

  await RNFetchBlob.fs.readFile(documentURI, 'base64');
joramirezStackit commented 2 years ago

I think I found the problem, in my case I had something like this "%20" (seems like an space) in the path I was using and I did something like this uri.replace('file://', '').replaceAll('%20', ' ') and it worked

vhonLopez commented 1 year ago

I think I found the problem, in my case I had something like this "%20" (seems like an space) in the path I was using and I did something like this uri.replace('file://', '').replaceAll('%20', ' ') and it worked

This one worked!!! saved my life.

ehbasouri commented 1 year ago

I think I found the problem, in my case I had something like this "%20" (seems like an space) in the path I was using and I did something like this uri.replace('file://', '').replaceAll('%20', ' ') and it worked

thanks, worked for me

vinod1988 commented 1 month ago

Thanks this solutions work