引用:https://blog.csdn.net/qq_26241443/article/details/104417957

在小程序中通过wx.openDocument预览文件,,如果想要分享给好友,,转发的时候是没有文件后缀的,,并且是随机生成的文件名

解决办法:
先通过FileSystemManager.saveFile() 把缓存文件保存下来,,就有后缀了

  function downloadAndPreviewPdf(id,title){
    console.log(title,22233333333)
    Taro.showLoading("下载中...")
    Taro.downloadFile({
      url:BASE_URL+"/mp/quote/generatePdf/"+id,
      header:{
        "mp_token":Taro.getStorageSync("token")
      },
      success:(res)=>{
        console.log(res,"res")

        if (res.statusCode === 200){
          const fs = Taro.getFileSystemManager()
          var filePath = res.tempFilePath;

          fs.saveFile({
            tempFilePath:filePath,
            filePath:Taro.env.USER_DATA_PATH + "/" +title+ "-"+Date.parse(new Date())+".pdf",
            success:function (res){
              Taro.openDocument({
                filePath:res.savedFilePath,
                fileType:"pdf",
                showMenu:true,
                success:()=>{
                  console.log("打开成功")
                }
              })
            }
          })

          // console.log("文件下载成功")
          // Taro.openDocument({
          //   filePath,
          //   fileType:"pdf",
          //   showMenu:true,
          //   success:()=>{
          //     console.log("打开成功")
          //   }
          // })
        }else{
          toast("下载失败")
        }
      },
      fail:(err)=>{
        console.error("下载出错", err);
        toast("下载失败")
      },
      complete: () => {
        Taro.hideLoading();
      }
    })
  }
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐