uni-app中uni.downloadFile()下载成功后,再用uni.openDocument()打开文件,安卓能正常打开但ios不能,报错912

uni-app中uni.downloadFile()下载成功后,再用uni.openDocument()打开文件,安卓能正常打开但ios不能,报错912

示例代码:

var url = urls.BASE_PATH + '/file/' + file.wjxxbsId;
console.log(url)
var fileType = file.wjlx;
console.log(fileType);
uni.showLoading({
title: '下载中'
});
uni.downloadFile({
url: encodeURI(url),
success: function (res) {
console.log(res)
var filePath = res.tempFilePath; // 小程序中文件的临时文件
uni.hideLoading();
uni.openDocument({
filePath: filePath,
fileType: String(fileType),
showMenu: true, // 允许出现分享功能
success: function (res) {
console.log(res);
console.log('打开文档成功')
},
fail: (e) => {
console.log(e);
}
})
}
})

操作步骤:

  • uni.downloadFile()
  • uni.openDocument()

预期结果:

  • 安卓和ios都能打开

实际结果:

  • 安卓能打开,ios不能报912

bug描述:

uni.openDocument()打开附件,安卓能正常打开但是ios不能,报912

测试是使用的附件是:xx.xlsx

猜测原因:安卓下载后的临时路劲带有后缀,但是ios没有

项目信息 信息
产品分类 uniapp/小程序/微信
PC开发环境操作系统 Windows
PC开发环境操作系统版本号 window10
HBuilderX类型 正式
HBuilderX版本号 2.6.5
第三方开发者工具版本号 v1.0.2.2004020
基础库版本号 2.9.2
项目创建方式 HBuilderX

Image Image


更多关于uni-app中uni.downloadFile()下载成功后,再用uni.openDocument()打开文件,安卓能正常打开但ios不能,报错912的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app中uni.downloadFile()下载成功后,再用uni.openDocument()打开文件,安卓能正常打开但ios不能,报错912的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这个问题是iOS平台在uni.openDocument()时常见的兼容性问题。主要原因是iOS系统对文件后缀名的严格校验。

解决方案:

  1. 确保下载的文件路径包含正确的文件后缀名:
uni.downloadFile({
  url: encodeURI(url),
  success: function (res) {
    let filePath = res.tempFilePath;
    // 为iOS添加文件后缀
    if(plus.os.name === 'iOS' && !filePath.endsWith('.'+fileType)){
      filePath = filePath + '.' + fileType.toLowerCase();
    }
    
    uni.openDocument({
      filePath: filePath,
      fileType: fileType.toLowerCase(),
      success: function (res) {
        console.log('打开文档成功');
      },
      fail: (e) => {
        console.log('打开失败', e);
      }
    })
  }
})
回到顶部