收藏
回答

wx.openDocument能直接打开base64格式的pdf文件嘛?

如果不能的话,是需要把base64数据转化为临时路径再打开嘛? 请问具体应该使用哪个api

回答关注问题邀请回答
收藏

1 个回答

  • Demons
    Demons
    01-19

    可以

    01-19
    有用
    回复 2
    • Thyme
      Thyme
      01-19
      我是这样实现的,请问有更简便的方法嘛?


      // 将 base64 数据保存为临时文件
                  wx.getFileSystemManager().writeFile({
                      filePath: wx.env.USER_DATA_PATH + '/temp.pdf',
                      data: app.globalData.mdmcAuthBase64, //  base64 数据
                      encoding: 'base64',
                      success: function () {
                          // 下载文件
                          wx.downloadFile({
                              url: wx.env.USER_DATA_PATH + '/temp.pdf', // 临时文件路径
                              success: function (res) {
                                  // 预览 PDF 文件
                                  wx.openDocument({
                                      filePath: res.tempFilePath,
                                      success: function (res) {
                                      console.log('打开文档成功', res);
                                      },
                                      fail: function (error) {
                                      console.error('打开文档失败', error);
                                      }
                                  });
                              },
                              fail: function (error) {
                                  console.error('下载文件失败', error);
                              }
                          });
                      },
                      fail: function (error) {
                      console.error('写入文件失败', error);
                      }
                  });
      01-19
      回复
    • ᴀ ᴶᴬᴺᴳᴼ
      ᴀ ᴶᴬᴺᴳᴼ
      04-10回复Thyme
      可以把downloadFile那一步省略,打开文档的filePath直接填写入时的path
      04-10
      回复
登录 后发表内容