uni-app uni.openDocument在iOS上报fail 文件没有发现
uni-app uni.openDocument在iOS上报fail 文件没有发现
产品分类:
uniapp/App
PC开发环境操作系统:
Mac
PC开发环境操作系统版本号:
10.15.5
HBuilderX类型:
正式
HBuilderX版本号:
3.1.18
手机系统:
iOS
手机系统版本号:
IOS 14
手机厂商:
苹果
手机机型:
iphone xs max
页面类型:
vue
打包方式:
云端
项目创建方式:
HBuilderX
示例代码:
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title" @tap='clickHandler'>导出</text>
</view>
</view>
</template>
<script>
let doExport = require('@/js_sdk/exportExcel/exportExcel.js');
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
clickHandler(){
var Excel_list = []
Excel_list.push([
{content: '序号',width: '200px',height: '25px',},
{content: '批次',width: '200px',height: '25px',},
{content: '发生日期',width: '200px',height: '25px',},
{content: '客户名称',width: '200px',height: '25px',},
{content: '店员',width: '200px',height: '25px',},
{content: '款号',width: '200px',height: '25px',},
{content: '名称',width: '200px',height: '25px',},
{content: '颜色',width: '200px',height: '25px',},
{content: '尺码',width: '200px',height: '25px',},
{content: '数量',width: '200px',height: '25px',},
{content: '发货情况',width: '200px',height: '25px',},
{content: '品牌',width: '200px',height: '25px',},
])
doExport('销售报表',Excel_list,this.ExportCall)
},
ExportCall(e){
var url = ''
if(plus.os.name == 'Android'){
url = '/storage/emulated/0'
}else if(plus.os.name == 'iOS'){
url = 'file://'
}
console.log(url+e);
uni.openDocument({
filePath:url+e,
success(res) {
console.log(res);
},
fail(e) {
console.log(e);
}
})
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
```
### 操作步骤:
本地生成一个Excel文件,然后使用openDocument打开,IOS下3.1.13版本可以正常打开文件,更新到3.1.18就报 `{"errMsg":"openDocument:fail 文件没有发现"}`
### 预期结果:
本地生成一个Excel文件,然后使用openDocument打开,IOS下3.1.13版本可以正常打开文件,更新到3.1.18就报 `{"errMsg":"openDocument:fail 文件没有发现"}`
### 实际结果:
本地生成一个Excel文件,然后使用openDocument打开,IOS下3.1.13版本可以正常打开文件,更新到3.1.18就报 `{"errMsg":"openDocument:fail 文件没有发现"}`
### bug描述:
```javascript
导出成功,文件位置:/var/mobile/Containers/Data/Application/E0575F5D-C442-479C-A5E2-FB18FED900FF/Documents/Pandora/documents/导出的Excel/20210617121042_销售报表/20210617121042_销售报表.xlsx at js_sdk/exportExcel/exportExcel.js:174
12:10:44.715 file:///var/mobile/Containers/Data/Application/E0575F5D-C442-479C-A5E2-FB18FED900FF/Documents/Pandora/documents/导出的Excel/20210617121042_销售报表/20210617121042_销售报表.xlsx at pages/index/index.vue:47
12:10:44.735 [Object] {"errMsg":"openDocument:fail 文件没有发现"} at pages/index/index.vue:54
```
更多关于uni-app uni.openDocument在iOS上报fail 文件没有发现的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于uni-app uni.openDocument在iOS上报fail 文件没有发现的实战教程也可以访问 https://www.itying.com/category-93-b0.html
已上传demo
解决了吗
养成好习惯 文件目录 用英文不要用中文和特殊字符 非要用的话 文件目录 文件名等 最好自己encode一下
在iOS 的真机系统下 在构建URL时 如果不对 中文做特殊处理 URL值为空
请问下载的文件使用中文标题,然后openducument的时候无法打开,这个该怎么处理,如果下载不使用中文标题,文件分享出去标题不好区分
这是一个iOS沙盒路径访问权限问题。在uni-app 3.1.18版本中,iOS对文件路径的访问权限要求更严格了。
问题出现在你的ExportCall方法中:
ExportCall(e){
var url = ''
if(plus.os.name == 'Android'){
url = '/storage/emulated/0'
}else if(plus.os.name == 'iOS'){
url = 'file://' // 这里有问题
}
console.log(url+e);
uni.openDocument({
filePath:url+e,
success(res) {
console.log(res);
},
fail(e) {
console.log(e);
}
})
}
对于iOS,不应该使用file://前缀。iOS沙盒环境下的文件路径应该直接使用相对路径或完整的沙盒路径。
修改方案:
ExportCall(e){
// 直接使用回调返回的文件路径,不需要添加前缀
console.log('文件路径:', e);
uni.openDocument({
filePath: e, // 直接使用e
success(res) {
console.log('打开文档成功', res);
},
fail(err) {
console.log('打开文档失败', err);
}
})
}
或者如果你需要确保路径正确,可以使用plus.io.convertLocalFileSystemURL方法:
ExportCall(e){
// 转换本地文件系统URL
let filePath = plus.io.convertLocalFileSystemURL(e);
console.log('转换后的文件路径:', filePath);
uni.openDocument({
filePath: filePath,
success(res) {
console.log('打开文档成功', res);
},
fail(err) {
console.log('打开文档失败', err);
}
})
}

