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

使用Expo选择CSV文档时出现路径不可读取错误

解决Expo DocumentPicker缓存文件无法读取的问题

可行解决方案

1. 先验证文件状态

读取前调用FileSystem.getInfoAsync确认文件存在且可读,排查文件本身的问题:

if (result.type === 'success') {
  const fileInfo = await FileSystem.getInfoAsync(result.uri);
  console.log(fileInfo); // 查看exists、isDirectory、size等状态字段
  if (fileInfo.exists) {
    let fileContent = await FileSystem.readAsStringAsync(result.uri, { encoding: 'utf8' });
    console.log(fileContent);
  } else {
    console.log("目标文件不存在");
  }
}

如果文件不存在,尝试移除copyToCacheDirectory: true参数(该参数默认值就是true,手动设置可能引发异常)。

2. 补充权限配置

在app.json的Android权限列表中添加外部存储读取权限,部分设备可能对缓存目录有额外限制:

{
  "expo": {
    "android": {
      "permissions": [
        "READ_EXTERNAL_STORAGE"
      ]
    }
  }
}

修改后重新构建项目生效。

3. 直接使用临时URI读取

去掉copyToCacheDirectory: true,直接使用DocumentPicker返回的原始临时URI读取文件,绕开缓存复制环节可能出现的问题:

let result = await DocumentPicker.getDocumentAsync({
  type: 'text/comma-separated-values',
  multiple: false,
});

if (result.type === 'success') {
  let fileContent = await FileSystem.readAsStringAsync(result.uri, { encoding: 'utf8' });
  console.log(fileContent);
}

4. 调整编码参数

如果CSV文件使用非UTF-8编码,尝试移除encoding: 'utf8'参数,让FileSystem自动适配编码:

let fileContent = await FileSystem.readAsStringAsync(result.uri);

额外排查步骤

若以上方法无效,尝试清理Expo项目缓存后重新运行:

expo r -c

同时确保expo-document-picker和expo-file-system是适配SDK48的最新版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 04:42:56