HarmonyOS NEXT下uni-app openDocument打开pdf文件失败,报{errMsg: "openDocument:fail Internal error."}
HarmonyOS NEXT下uni-app openDocument打开pdf文件失败,报{errMsg: “openDocument:fail Internal error.”}
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Mac | 15.1.1 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Mac
HBuilderX类型:正式
HBuilderX版本号:4.45
手机系统:HarmonyOS NEXT
手机系统版本号:HarmonyOS NEXT Developer Beta2
手机厂商:模拟器
手机机型:模拟器
页面类型:vue
vue版本:vue3
打包方式:离线
项目创建方式:HBuilderX
### 示例代码:
```p
uni.downloadFile({
url: 'https://example.com/somefile.pdf',
success: function (res) {
var filePath = res.tempFilePath;
uni.openDocument({
filePath: filePath,
showMenu: true,
success: function (res) {
console.log('打开文档成功');
}
});
}
});
15:09:50.855 /data/storage/el2/base/temp/HBuilder/download/1737356989926.pdf
15:09:51.889 {errMsg: "openDocument:fail Internal error."}
下载的pdf,openDocument打开失败
操作步骤:
无
预期结果:
打开文件
实际结果:
报错
bug描述:
uni.downloadFile下载的pdf,openDocument打开失败,报{errMsg: "openDocument:fail Internal error."}
更多关于HarmonyOS NEXT下uni-app openDocument打开pdf文件失败,报{errMsg: "openDocument:fail Internal error."}的实战教程也可以访问 https://www.itying.com/category-93-b0.html
开发工具更新了一下Hx4.51.2025010708-dev,竟然能打开了,不知道什么原因。
更多关于HarmonyOS NEXT下uni-app openDocument打开pdf文件失败,报{errMsg: "openDocument:fail Internal error."}的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
可能是下个版本更新了这个方式,这个方法在 4.51 有一个调整是:这个接口使用了不同的打开 Document 的方式,之前是ohos.want.action.sendData,根据华为的建议,改成了:ohos.want.action.viewData,这个方式打开文档是直接通过系统的默认的打开文件方式打开,而不用经过一个选择打开方式的步骤。
一般是这个问题的调整,再最近这个地方也没有修改了
大佬 我在matepad上调试,然后uni.openDocument能打开弹窗 选应用,但是为什么没有wps选项,我已经下载了wps的,弹窗里甚至有抖音小红书 选项
回复 1***@qq.com: 这个你需要查看是不是升级到 4.51 才可以,注意 oh-package 是不是有之前版本信息
这个错误是由于HarmonyOS NEXT系统对文件访问权限的限制导致的。在HarmonyOS NEXT下,uni-app的openDocument接口需要特殊处理才能正常工作。
解决方案:
- 确保下载的文件路径正确,可以使用plus.io获取系统合法路径:
const filePath = plus.io.convertLocalFileSystemURL(res.tempFilePath);
- 完整修正后的代码示例:
uni.downloadFile({
url: 'https://example.com/somefile.pdf',
success: function (res) {
const filePath = plus.io.convertLocalFileSystemURL(res.tempFilePath);
uni.openDocument({
filePath: filePath,
fileType: 'pdf',
success: function (res) {
console.log('打开文档成功');
},
fail: function (err) {
console.error('打开文档失败:', err);
}
});
}
});