欢迎光临,了解微信小程序开发,就上易用通!

微信小程序下载预览PDF

发布:2021-06-23 14:26浏览: 来源:网络 作者:huan

微信小程序的文件预览需要先使用wx.downloadFile下载文件,然后使用下载文件的临时路径通过wx.openDocument进行文件的预览

项目中,有的需要打开pdf 来阅读信息。就需要用小程序自带的api方法。

wxml代码:


<button  bindtap='preview'>PDF文件预览</button>

js代码:

//PDF预览
  preview: function () {
    var that = this;
    console.log("PDF预览")
     
//这里的value是先在data里面初始化,然后我根据用户切换单选框,获取的PDF文件的主键id
    console.log(this.data.value)
    var id = that.data.value;
 
    if (id == "") {
      wx.showModal({
        title: '',
        content: '请选择一份PDF',
        showCancel: false,
        confirmColor: "#FFB100"
      })
    } else {
 
      //先通过PDF的主键id,查询PDF路径(大家可以根据自己的需求来传数据)
      wx.request({
        url: app.globalData.url + "/api/interview/queryFilePath",
        data: {
          id: id
        },
        method: 'POST',
        header: { "content-type": "application/x-www-form-urlencoded" },
        success: function (res) {
          console.log(res.data)
          that.setData({
            path: res.data.path,
            type: res.data.type
          })
          //下载PDF
          wx.downloadFile({
            url: app.globalData.url + that.data.path,
            success: function (res) {
              var filePath = res.tempFilePath
              console.log(filePath)
 
              //预览PDF
              wx.openDocument({
                filePath: filePath,
                fileType: that.data.type,
                success: function (res) {
                  console.log("打开文档成功")
                  console.log(res);
                },
                fail: function (res) {
                  console.log("fail");
                  console.log(res)
                },
                complete: function (res) {
                  console.log("complete");
                  console.log(res)
                }
              })
            },
            fail: function (res) {
              console.log('fail')
              console.log(res)
            },
            complete: function (res) {
              console.log('complete')
              console.log(res)
            }
          })
        }
      })
    }
  },




免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。