You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用react-native-document-picker与RNFS转文件为Base64时iPhone本地文件报ENOENT

问题排查与解决方案

核心原因

iOS端选择「On My iPhone」本地文件时报错是两个路径匹配问题导致的:

  • react-native-document-picker返回的uri默认携带file://协议头,而RNFS的readFile接口在iOS环境下要求传入无协议头的纯路径,直接传入uri会触发路径识别失败
  • 返回的uri对特殊字符做了URL编码(比如示例路径里的空格被转义为%20),未解码的转义字符也会导致系统无法找到对应文件

修复代码

对返回的uri做两步处理后再传入RNFS即可:

const response = await DocumentPicker.pickSingle({
  type: [DocumentPicker.types.allFiles],
  copyTo: "documentDirectory" 
});
// 移除file://协议头
let targetPath = response.uri.replace(/^file:\/\//, '');
// 解码URL转义字符
targetPath = decodeURIComponent(targetPath);
// 读取文件转base64
const data = await RNFS.readFile(targetPath, 'base64');

额外检查项

如果修改后仍有报错,确认两项原生配置:

  • 项目Info.plist中已添加UIFileSharingEnabled(应用支持iTunes文件共享)和LSSupportsOpeningDocumentsInPlace(应用支持打开本地文件)两个权限,且值均为YES
  • react-native-document-picker版本不低于v8.0.0,低版本存在copyTo参数不生效的已知bug,建议升级到最新稳定版

内容的提问来源于stack exchange,提问作者Tanveerbyn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 17:54:03