reactjs - 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')]
解决方案
您使用了错误的功能。
FileSystem.writeAsStringAsync 用于将内容写入文件。
您想要的是读取文件的内容。
您应该改用 FileSystem.readAsStringAsync。
推荐阅读
- c++ - 在 Linux 中快速制作 Armadillo C++ 库 - Ubuntu
- c# - 使用 Stripe 的 Payment Intent API 接受 Xamarin 表单中的付款
- apache-camel - Camel-JoSQL 对骆驼 3 的支持
- python - 函数 Django 中的 total_likes 导致的错误
- c# - 使用具有恒定值的外键的地图集合
- javascript - 使用 VueJS 从 JSON 结果中添加表行?
- bash - if 语句中的回车符和通配符
- javascript - 由信号 SIGSEGV 终止的命令行“deno run server.ts”中的错误(地址边界错误)
- regex - 沙特国民身份证号码正则表达式
- c - 评估顺序是什么?