3

I'm using a PDF viewer on react-native, some files without : can be opened but other files with : won't open and will return No content provider.

Is there a way to read locally stored files with : on it's name on react-native?

For example, this kind of file path

const filenameWithSpace = 'file:///storage/emulated/0/somwhere/foo foo Nov 21, 2018 12:16:04 PM.pdf'

I already tried encodeURIComponent and encodeURI but it didn't work.

Update: The problem on the filename was actually the : character not the spaces.

13
  • Hey kcNeko, have you tried adding %20 wherever you need a space in the url? Commented Nov 29, 2018 at 4:18
  • which plugin you are using to view pdf files? Commented Nov 29, 2018 at 4:18
  • @GrandIQ I already tried replacing all spaces with %20 don't it didn't do the trick. Commented Nov 29, 2018 at 4:21
  • @Justcode I'm working with react-native-pdf :) Commented Nov 29, 2018 at 4:22
  • 1
    Alright well I tried my best, definitely staring this I want to know how you resolved it! Commented Nov 29, 2018 at 4:56

4 Answers 4

6

If space in file uri this will fail.

var base64data = await RNFS.readFile( res.uri, 'base64').then(res => { return res });

Replace with this line

var result = res.uri.split("%20").join("\ ");
var base64data = await RNFS.readFile( result, 'base64').then(res => { return res });

This will work

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

Comments

2

Have you tried using '\' (backslash)?

Please let me know if this works.

var str = "file:///storage/emulated/0/somwhere/foo foo Nov 21, 2018 12:16:04 PM.pdf";
    var result = str.split(" ").join("\ "); // added '\'
    console.log('Result',result);

Snippet copied from answer provided by Vishal Vaghasiya

4 Comments

Sorry, I updated the question and it seems like spaces on file names are okay but not the : character :(
tricky one though.. Good to hear that your issue is solved :)
It's not really solved but pointed out who the real culprit is XD
Thanks @HungrySoul your solution have worked for me so far in ios. i will check it further.
1

I wasn't able to explain the whole process of the app.

The return from database has something like this

{file: "//storage/emulated/0/somwhere/foo foo Nov 21, 2018 12:16:04 PM.pdf"}

which I use to read with my PDF library. However the actual file that is stored locally has a name of foo foo Nov 21, 2018 121604 PM.pdf

so I have to do this instead

'file://' + file.replace(/:/g, "")

or it can be done like this

'file://' + file.replace(/[|&:;$%@"<>()+,]/g, "")

Comments

-1
var str = "file:///storage/emulated/0/somwhere/foo foo Nov 21, 2018 12:16:04 PM.pdf";
        var result = str.split(" ").join("");
        console.log('Result',result);

file:///storage/emulated/0/somwhere/foofooNov21,201812:16:04PM.pdf

7 Comments

If I understand correctly, OP has a problem with file names that already have a space in the local directory.
you say ; files without spaces can be opened
Yah, so removing spaces wouldn't be a problem. OP also tried adding %20 manually and still the same outcome sadly.
give me static output in this url
give me excatly output file:///storage/emulated/0/somwhere/foofooNov21,201812:16:04PM.pdf
|

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.