微信小程序pdf预览

       let _that = this;
        const fileName = result['Retdata']['PDFUrl']; //获取页面要下载的PDF文件
        const fileExtName = ".pdf";
        const randfile = this.randomString(32) + new Date().getTime() + fileExtName;
        const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
        wx.downloadFile({
          url: fileName,
          header: {
            "Content-Type": "application/pdf"
          },
          filePath: newPath,
          success: (res) => {
            console.log(res);
            var filePath = res.tempFilePath;
            // console.log("filePath", filePath);
            wx.openDocument({
              filePath: newPath,
              showMenu: true,
              fileType: 'pdf',
              success: function (res) {
                _that.setData({
                  hidden: false
                })
                console.log('打开文档成功', res)

              },
              fail: function (res) {
                console.log("res", res);
                wx.showToast({
                  title: '文件格式不支持预览',
                })
              }
            })
          },
          fail: (err) => {
            console.log(err, "下载失败")
          }
        })
  randomString: function (e) {
    e = e || 32;
    var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
      a = t.length,
      n = "";
    for (var i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
    return n
  },

完事

你可能感兴趣的:(微信小程序,javascript,pdf预览,微信小程序,javascript,前端)