苹果手机ios系统中wx.openDocument API 打开doc、xls、pdf、ppt等文件调用成功但是打开文件无反应/空白、或者显示文件已过期或被清理

原来的代码时和大家一样 网上搜解决方案有说加上这个指定/类型ios就能打开但是我试了不行

let state = e.currentTarget.dataset.suffix   //文档类型doc\pdf\word\ppt等
let fileUrl = e.currentTarget.dataset.fileurl  //接口


    wx.downloadFile({
      url: fileUrl, //仅为测试接口
      success: function (res) {
        var filePath = res.tempFilePath
        console.log(filePath)

        wx.openDocument({
          filePath: filePath,
          fileType: state,              //网上搜解决方案有说加上这个指定
          success: function (res) {     //类型ios就能打开但是我试了不行
            console.log("打开文档成功")
            console.log(res);
          },
          fail: function (res) {
            console.log("fail");
            console.log(res)
          },
          complete: function (res) {
            console.log("complete");
            console.log(res)
          }
        })
      },
      fail: function (res) {
        console.log('fail')
        console.log(res)
      },
      complete: function (res) {
        console.log('complete')
        console.log(res)
      }
    })

改成如下就ok ,本来是一个虚拟的远程地址不知道为啥ios系统就是解析不了,官方从19年开始有人问他就是不改。换种储存方式,改成本地储存的就行了。

 wx.downloadFile 添加 filePath: wx.env.USER_DATA_PATH + "/" + name

 var filePath = res.tempFilePath 改成 var filePath = res.filePath 就解决了多传一个name字段

let state = e.currentTarget.dataset.suffix   //文档类型doc\pdf\word\ppt等
let fileUrl = e.currentTarget.dataset.fileurl  //接口
let name = e.currentTarget.dataset.name      //文件名


    wx.downloadFile({
      url: fileUrl, //仅为测试接口
      filePath: wx.env.USER_DATA_PATH + "/" + name, //name是要打开文件的文件名称
      success: function (res) {
        var filePath = res.filePath
        console.log(filePath)

        wx.openDocument({
          filePath: filePath,
          fileType: state,              //网上搜解决方案有说加上这个指定
          success: function (res) {     //类型ios就能打开但是我试了不行
            console.log("打开文档成功")
            console.log(res);
          },
          fail: function (res) {
            console.log("fail");
            console.log(res)
          },
          complete: function (res) {
            console.log("complete");
            console.log(res)
          }
        })
      },
      fail: function (res) {
        console.log('fail')
        console.log(res)
      },
      complete: function (res) {
        console.log('complete')
        console.log(res)
      }
    })

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果您不想使用 `uni.openDocument` API 打开文件,您可以考虑使用其他方式打开文件,比如使用第三方插件或者自定义组件等。 以下是两个示例: 1. 使用第三方插件: 可以使用 uni-app 社区的一些第三方插件来实现打开文件的功能,例如 `uni-file-picker` 插件。这个插件可以选择文件并返回文件的本地路径,您可以使用这个路径来打开文件。 安装插件: ``` npm install uni-file-picker --save ``` 使用插件: ```javascript import filePicker from 'uni-file-picker' // 选择文件 filePicker.chooseFile({ success: (res) => { // res.tempFilePaths 为文件的本地临时路径 // 这里可以使用自己的方式打开文件 } }) ``` 2. 自定义组件: 您可以自定义一个组件来实现打开文件的功能。在组件,可以使用 `wx.chooseMessageFile` API 选择文件并返回文件的临时路径,然后使用 `wx.openDocument` API 打开文件。 组件示例: ```html <template> <button @click="openFile">打开文件</button> </template> <script> export default { methods: { openFile() { wx.chooseMessageFile({ count: 1, type: 'file', success: (res) => { wx.openDocument({ filePath: res.tempFiles[0].path, fileType: 'txt', success: () => { console.log('打开文件成功') }, fail: () => { console.log('打开文件失败') } }) }, fail: () => { console.log('选择文件失败') } }) } } } </script> ``` 注意:在使用 `wx.chooseMessageFile` 和 `wx.openDocument` API 时,需要将 `type` 和 `fileType` 参数设置为要打开文件类型,这里以打开 txt 文件为例,需要将类型设置为 `file`,文件类型设置为 `txt`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值