微信小程序wx.openDocument真机无法打开的问题解决方法

微信小程序wx.opendocument无法打开文件
大家看过微信小程序文档就知道他有一个文件类型叫做fileType 在文档中说的是 不是必填项 但是 你不填就会报错 简直有毒

但是 你填写 就只能打开那一种格式的文件

我下面来给 大家 带来一种解决方法 就是 将后台传给你的 文件的 后缀名 截取下来动态的放进去

wx.downloadFile({
      url: fileurl,
      success: function (res) {
        // console.log(res)
        var filePath = res.tempFilePath              //返回的文件临时地址,用于后面打开本地预览所用
        var filename = fileurl;
        var index1 = filename.lastIndexOf(".");
        var index2 = filename.length;
        var postf = filename.substring(index1, index2);//后缀名
        var postf1 = postf.replace(/\./g, '')
        // console.log(postf1)
        if (filePath != null) {
          wx.openDocument({
            filePath: filePath,
            fileType: postf1,
            success: function (res) {
              console.log(res)
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }
      },
      fail: function (res) {
        console.log(res)
      }
    })

如果 在真机 上 还是 无法打开 请打开 你的 微信开发者平台

看一下 你的 uploadfile 和 downloadfile 的 合法 域名 填写 了没
不填写的 话 会导致 你的 文件 无法打开

发布了47 篇原创文章 · 获赞 22 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wuwenjie_1997/article/details/105094581
今日推荐