微信小程序打开pdf

wxml

<block  
      wx:for="{{list}}" 
      wx:key="id" 
      wx:for-item="item"
            wx:for-index="index">
      
        <view class="list_item" bindtap="downLoadPDF" data-url="{{item.filePath}}">
          <view class="item_name">
            {{item.title}}
          </view>
          <view class="item_date">
            {{item.publishDate}}
          </view>
        </view>
      </block>

js

//下载PDF文件 
    downLoadPDF(e){
      let url = e.currentTarget.dataset.url;
      // wx.showModal({
      //   title: '温馨提示',
      //   content: '确认要打开此PDF文件吗?',
      //   showCancel: true,
      //   cancelText: '取消',
      //   confirmText: '确定',
      //   success: (result) => {
      //     if (result.confirm) {
            wx.downloadFile({
              url: url,    
              success: function (resinfo) {
                  console.log("pdf协议文件已下载")
                  let path = resinfo.tempFilePath;
                  console.log(path, resinfo)
                  wx.openDocument({
                      filePath: path,
                      fileType: 'pdf',
                      success: function (rest) {
                          console.log('打开文件成功')
                          console.log(rest);
                      },
                      fail: function (error) {
                          wx.showToast({
                              icon: 'none',
                              title: '打开文件失败'
                          });
                      },
                  })
              },
              fail: function (err) {
                  console.log('fail')
                  console.log(err)
                  wx.showToast({
                    icon: 'none',
                    title: '下载文件失败'
                });
              }
          })     
      //     }
      //   },
      //   fail: () => {},
      //   complete: () => {}
      // })
    },

 

posted @ 2023-09-19 11:21  土小狗  阅读(209)  评论(0编辑  收藏  举报