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: () => {}
      // })
    },

url:示例:http://ip:port/3143cc28-dc01-4799-8463-3211c19b46f3.pdf

 

posted on 2023-02-15 17:56  周文豪  阅读(403)  评论(0编辑  收藏  举报