uni-app uni.saveFile IOS下载附件失败

uni-app uni.saveFile IOS下载附件失败

开发环境 版本号 项目创建方式
Windows 1158 HBuilderX

产品分类:
uniapp/App

PC开发环境操作系统:
Windows

HBuilderX类型:
正式

HBuilderX版本号:
3.1.18

手机系统:
iOS

手机系统版本号:
iOS 13.4

手机厂商:
苹果

手机机型:
IPhone X

页面类型:
vue

打包方式:
云端

示例代码:

uni.downloadFile({
url: singleDownload,
header: headerConfig,
success(res) {
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(saveSucc) {
var savedFilePath = saveSucc.savedFilePath;
uni.showToast({
icon: 'none',
title: '下载完成,保存路径为:' + savedFilePath
});
uni.openDocument({
filePath: savedFilePath,
});
},
fail: (err) => {
console.error('下载错误')
console.error(err)
}
})
}
})

操作步骤:

uni.downloadFile({
url: singleDownload,
header: headerConfig,
success(res) {
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(saveSucc) {
var savedFilePath = saveSucc.savedFilePath;
uni.showToast({
icon: 'none',
title: '下载完成,保存路径为:' + savedFilePath
});
uni.openDocument({
filePath: savedFilePath,
});
},
fail: (err) => {
console.error('下载错误')
console.error(err)
}
})
}
})

预期结果:
正常打开

实际结果:
打开失败

bug描述:
uni.downloadFile 下载成功之后 保存在uni.saveFile本地路径是报错 如图所示


更多关于uni-app uni.saveFile IOS下载附件失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app uni.saveFile IOS下载附件失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在iOS系统中,uni.saveFile 保存失败通常与文件路径格式或权限问题相关。从你的代码来看,uni.downloadFile 下载成功后,res.tempFilePath 可能不是有效的临时文件路径,或者iOS系统对文件保存位置有权限限制。

检查以下几点:

  1. 文件路径有效性:确保 res.tempFilePath 是有效的临时文件路径。在iOS中,临时文件路径可能因系统版本或沙盒机制而变化。可以通过 console.log(res.tempFilePath) 输出路径,确认其是否以 file:// 开头或符合iOS文件系统规范。
  2. 文件类型支持:iOS对某些文件类型(如非标准格式)的保存和打开可能有限制。确认下载的文件类型(如PDF、图片等)是否受 uni.openDocument 支持。
  3. 权限配置:在 manifest.json 中检查是否配置了文件存储权限。例如,添加以下配置:
    "ios" : {
      "permissions" : {
        "Documents" : true
      }
    }
    
  4. 路径处理:iOS保存路径可能需要使用绝对路径。尝试使用 plus.io.convertLocalFileSystemURL 转换路径:
    var savedFilePath = plus.io.convertLocalFileSystemURL(saveSucc.savedFilePath);
    
  5. 错误日志:在 fail 回调中打印具体错误信息(如 err.errMsg),帮助定位问题。常见错误包括路径无效、存储空间不足或系统拦截。

如果问题仍存在,可能是iOS沙盒机制限制文件访问。尝试使用 uni.downloadFile 直接打开文件,避免保存步骤:

uni.downloadFile({
  url: singleDownload,
  success(res) {
    uni.openDocument({
      filePath: res.tempFilePath
    });
  }
});
回到顶部