微信小程序打开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 @   土小狗  阅读(217)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2022-09-19 送给自己和大家的三句话
点击右上角即可分享
微信分享提示