uni-app 新开页面打开文档 uni.openDocument(OBJECT) 以及第一次可以打开 第二次无法打开的问题

文档地址  https://uniapp.dcloud.io/api/file/file.html#getfileinfo

新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。

 uni.downloadFile({
          url: url,
          success: function (res) {
            var filePath = res.tempFilePath;
            var sidx = filePath.lastIndexOf('(');
            var eidx = filePath.lastIndexOf(')');
            //  用于处理文档打开问题(ps:第一次打开 第二次无法打开问题)
            if (sidx > -1 && eidx > -1) {
              var restr = filePath.substr(sidx, eidx - sidx + 1);
              filePath = filePath.replace(restr, '');
            }
            setTimeout(
              () =>
                uni.openDocument({
                  filePath: escape(filePath),
                  showMenu: false,
                  success: function () {
                    console.log("打开文档成功");
                  },
                  fail: function () {
                    uni.showToast({
                      title: '暂不支持此类型',
                      duration: 2000,
                      icon: "none",
                    });
                  }
                }),
              1000
            );
          },
          fail: function (res) {
            console.log(res); //失败
          }
        });

 

posted @ 2022-05-17 15:55  蓝色精灵jah  阅读(2500)  评论(0编辑  收藏  举报