微信小程序文件预览下载,转发功能

692 阅读1分钟

在微信小程序文件预览后转发左上角的扩展按钮不展示,转发后无后缀和后缀对应不上问题,问题存在微信的后缀是对应的,但安卓的后缀不能够对应

/* 点击按钮自定义 */

 onOpenFile() {
      uni.downloadFile({
      /* 下载文件路径  */
        url: ".xlsx",
        success: function (res) {
          const tempFilePath = res.tempFilePath;
          
          /* 文件下载下来后先检查本地受否有历史文件,如果有先删除,避免内存不够*/
          uni.getSavedFileList({
            success: function (res) {
              if (res.fileList.length > 0) {
                res.fileList.forEach((i) =>
                  uni.removeSavedFile({
                    filePath: i.filePath,
                    complete: function (res) {
                      console.log(res);
                    },
                  })
                );
              }
            },
          });
			//文档中存入名称需要自定义,解决安卓转发后缀不同的问题
          const filePath = wx.env.USER_DATA_PATH + "/" + Date.parse(new Date()) + ".xlsx";
            
          uni.saveFile({
            filePath,
            tempFilePath: tempFilePath,
          /* 下载下来存储路径,我们需要将他存入本地,这样转发后就是有后缀的文件 */
            success: function (res1) {
              var savedFilePath = res1.savedFilePath;
              uni.openDocument({
                filePath: savedFilePath,
                //转发按钮的展示
                showMenu: true,
                success: function (res2) {
                  console.log(res2);
                  uni.hideLoading();
                  console.log(res2);
                },
                fail(err2) {
                  uni.hideLoading();
                  console.log(err2);
                },
              });
            },
            fail(err2) {
              uni.hideLoading();
              uni.showToast({
                icon: "none",
                title: "文件太大!文件不能超出10M",
              });
            },
          });
        },
        fail(err2) {
          uni.hideLoading();
          console.log(err2);
        },
      });
    }