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

Expo DownloadAsync iOS端无法下载文件名含空格的Firebase文件

问题原因

这是iOS端文件路径的URI编码兼容问题:

  • iOS端FileSystem.downloadAsync不会自动对本地保存路径中的特殊字符做百分号转义,路径里出现未编码的空格时,系统会把空格识别为路径终止标记,直接截断路径,最终导致文件写入失败、对应文件被跳过
  • Android端的文件系统接口默认会自动转义路径中的空格等特殊字符,所以不会出现该异常
  • 你已经确认Firebase可正常返回文件访问链接,可排除存储侧故障,问题完全出在本地路径处理环节。
修复方法

对传入downloadAsync的本地目标路径做URI编码,转义空格等不符合URI规范的特殊字符即可。核心修改是对拼接完成的本地文件路径调用encodeURI处理,修复后的完整代码如下:

await getDownloadURL(
   ref(
      storage,
      `/images/objects/${touristObject.id}/${file}`
   )
)
.then(async (url) => {
   const fileReference = 
      `${FileSystem.documentDirectory}images/objects/${touristObject.id}`

   await FileSystem.makeDirectoryAsync(
      fileReference,
      { intermediates: true }
   )
                                      
   // 转义本地路径中的空格等特殊字符,适配iOS端路径规则
   const localTargetPath = encodeURI(`${fileReference}/${file}`)
   
   await FileSystem.downloadAsync(
      url,
      localTargetPath
   )
   .then((uri) => { resolve() })
   .catch((error) => {
      console.warn(error)
      resolve()
   })
})
.catch((error) => {
   console.warn(error)
   resolve()
})
额外建议
  • 不建议单独用字符串替换把空格手动换成%20,encodeURI会自动覆盖所有iOS路径不兼容的特殊字符(包括中文、各类特殊符号),能避免后续其他文件名触发同类问题
  • 当前代码混用了async/await和.then()链式调用,虽然不影响功能运行,但统一成async/await写法可以减少代码嵌套,提升可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 03:39:34