首页 > 解决方案 > 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')]

标签: reactjsreact-nativereact-native-androidexporeact-native-ios

解决方案


您使用了错误的功能。

FileSystem.writeAsStringAsync 用于将内容写入文件。

您想要的是读取文件的内容。

您应该改用 FileSystem.readAsStringAsync。


推荐阅读