javascript - 在 expo Document-picker 上使用 URI 以獲取文件本身
我在嘗試從 react-native(使用 Expo 的 Android)上傳文件到後端伺服器時遇到了困難。我從 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",
}
我現在的問題是如何使用 URI 來獲取檔案本身?這樣我才能將它傳送到後端伺服器
謝謝
答案#1
你有試過 ExpoFileSystem.readAsStringAsync 嗎?
例如,已經有了你提到的 URI:
import * as ExpoFileSystem from 'expo-file-system'
...
const fileContent = await ExpoFileSystem.readAsStringAsync(uri);
也許你想把它從 JSON 轉換成其他格式:
const content = JSON.parse(fileContent);
