【问题标题】:react native Expo convertir file:// a base64react native Expo convertir file://a base64
【发布时间】:2020-08-22 06:31:23
【问题描述】:

我正在尝试将 file:// 转换为 base64,我得到了所选文件的 uri (file: // ...),但是当通过 FileSystem 传递它以转换为 base64 时我遇到了问题

 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)
}

在第一个 console.log (file.uri) 我得到文件的 uri

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

在我应该转换为 base64 的第二个 console.log (fileBase64) 中

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

【问题讨论】:

  • 请使用英文

标签: reactjs react-native react-native-android expo react-native-ios


【解决方案1】:

试试吧!为我工作

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

【讨论】:

    【解决方案2】:

    你使用了错误的函数。

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

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

    您应该改用 FileSystem.readAsStringAsync。

    【讨论】:

      猜你喜欢
      • 2019-07-12
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 2022-12-27
      • 2020-08-09
      • 2020-03-15
      • 2018-02-12
      • 1970-01-01
      相关资源
      最近更新 更多