zoukankan      html  css  js  c++  java
  • 微信小程序文件预览源码(实用--打开pdf)

    微信小程序的文件预览

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

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

    wxml代码:

    <button  bindtap='preview'>工作简历预览</button>

    js代码:

    //简历预览
      preview: function () {
        var that = this;
        console.log("简历预览")
    	
    //这里的value是先在data里面初始化,然后我根据用户切换单选框,获取的简历文件的主键id
        console.log(this.data.value)
        var id = that.data.value;
    
        if (id == "") {
          wx.showModal({
            title: '',
            content: '请选择一份简历',
            showCancel: false,
            confirmColor: "#FFB100"
          })
        } else {
    
          //先通过简历的主键id,查询简历路径(大家可以根据自己的需求来传数据)
          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
              })
              //下载简历
              wx.downloadFile({
                url: app.globalData.url + that.data.path,
                success: function (res) {
                  var filePath = res.tempFilePath
                  console.log(filePath)
    
                  //预览简历
                  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)
                }
              })
            }
          })
        }
      },
    

      

    爱生活、爱编程!
  • 相关阅读:
    python分布式爬虫-Selenium(针对需要渲染的页面)
    《PHP高性能开发:基础、框架与项目实战》_王甲临
    flutter hello world
    C#+EF+SQLite数据库操作
    【转】Wilcoxon 检验之 rank-sum 与 signed-rank
    [转]多目标进化算法的性能指标总结 (一)
    IDEA查找栏
    [转]IDEA空指针断点
    Java 高斯分布随机数
    【Vegas原创】centos中挖矿病毒kdevtmpfsi的终极解决方法
  • 原文地址:https://www.cnblogs.com/liliuyu/p/11634263.html
Copyright © 2011-2022 走看看