BandG
BandG

Reputation: 11

react native Expo convertir file:// a base64

I am trying to convert a file:// to base64, I get the uri (file: // ...) of the selected file but when passing it through FileSystem to convert to base64 I have problems

 pdf = async () => {
    let file = await DocumentPicker.getDocumentAsync({ type: "application/pdf", copyToCacheDirectory: true, multiple: true });

    console.log(file.uri)

   let fileBase64 = await FileSystem.writeAsStringAsync(file.uri, { encoding: FileSystem.EncodingTypes.Base64  });

    console.log(fileBase64)
}

in the first console.log (file.uri) I get the uri of the file

file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Fasd-6a4b4e8e-3a0f-43b8-b1ds94-99f8ac7/DocumentPicker/9cf51edfab9-5185-411f-a397-ef10633sdf7324f.pdf

in the second console.log (fileBase64) where I should convert to base64 I get

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'FileSystem.EncodingTypes.Base64')]

Upvotes: 1

Views: 2897

Answers (2)

Manoel Pereira Leno
Manoel Pereira Leno

Reputation: 49

Just try it! works for me

let fileBase64 = await FileSystem.readAsStringAsync(file.uri, { encoding: 'base64'  });

Upvotes: 1

monsty
monsty

Reputation: 633

You are using the wrong function.

FileSystem.writeAsStringAsync is used to write content into a file.

What you want is to read the content of a file.

You should use FileSystem.readAsStringAsync instead.

Upvotes: 1

Related Questions