在 expo Document-picker 上使用 URI 来获取文件本身

Pat*_*ack 5 javascript react-native expo

我在尝试从react-native(android使用expo)上传文件到后端服务器时遇到了麻烦。我从DocumentPicker.getDocumentAsync()这个回复中得到

    {
      "name": "QuitusInscription_30769_59.pdf",
      "size": 41438,
      "type": "success",
      "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Fcsdl_frontend-46ddf63e-90ab-44b3-a4c7-6ad7aad5cccc/DocumentPicker/a40a6599-4900-439a-9e9b-57fa555a80c9.pdf",
    }
Run Code Online (Sandbox Code Playgroud)

我现在的问题是如何使用 uri 来获取文件本身?这样我就可以将其发送到后端服务器

谢谢

小智 6

您尝试过ExpoFileSystem.readAsStringAsync吗?

例如,已经有了你所说的 uri:

import * as ExpoFileSystem from 'expo-file-system'
...
const fileContent = await ExpoFileSystem.readAsStringAsync(uri);
Run Code Online (Sandbox Code Playgroud)

也许您想将它从 JSON 转换为另一种格式:

const content = JSON.parse(fileContent);
Run Code Online (Sandbox Code Playgroud)