• uniapp 手机端实现pdf文件下载和预览


    以下代码兼容三端,app,h5,微信小程序,经过个人测试
    手机端有两种方法,使用renderjs或者uniapp的api
    两者的区别

    • 使用renderjs的写法,会提示用户是否下载文件,下载完成后用户需要手动点击下载的文件,才会打开文件
    • 使用uniapp的api则可以直接下载并直接预览,不需要用户操作
    • 根据场景需求进行选择即可
    • ios需要注意中文名称的文件需要转码https://blog.csdn.net/weixin_45122120/article/details/107956432
    <template>
      <div>
        
        <button @click="test.exportPDF">预览和下载pdf(renderjs)button>
        <button @click="exportPDF">预览和下载pdf(uniapp api)button>
        
        
        <button @click="exportPDF">预览和下载pdfbutton>
        
      div>
    template>
    
    
    <script module="test" lang="renderjs">
    export default {
      methods: {
        exportPDF() {
          const Url = "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7da443bc-353a-4224-ab27-b98917aa6c66/89d1d612-734a-4219-9110-0b21fb004d5f.pdf"
          const a = document.createElement("a")
          a.href = Url
          a.download = "download"
          a.click()
        }
      }
    }
    script>
    
    
    <script>
    export default {
      methods: {
        exportPDF() {
          //  #ifdef H5
          window.open(
            "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7da443bc-353a-4224-ab27-b98917aa6c66/89d1d612-734a-4219-9110-0b21fb004d5f.pdf"
          )
          // #endif
    
          // 微信下载文件需要在微信公众平台>开发>开发管理>服务器域名>downloadFile合法域名>配置白名单域名
          // #ifdef MP-WEIXIN
          uni.downloadFile({
            url:
              "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7da443bc-353a-4224-ab27-b98917aa6c66/89d1d612-734a-4219-9110-0b21fb004d5f.pdf",
            success: res => {
              console.log(res)
              if (res.statusCode === 200) {
                // 预览pdf文件
                uni.openDocument({
                  filePath: res.tempFilePath,
                  showMenu: true, // 右上角菜单,可以进行分享保存pdf
                  success: function(file) {
                    console.log("file-success", file)
                  }
                })
              }
            }
          })
          // #endif
    
          // #ifdef APP-PLUS
          uni.downloadFile({
            url:
              "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7da443bc-353a-4224-ab27-b98917aa6c66/89d1d612-734a-4219-9110-0b21fb004d5f.pdf",
            success: res => {
              console.log(res)
              if (res.statusCode === 200) {
                // 保存pdf文件至手机,一般安卓端存储路径为:手机存储/dcim/camera文件夹下
                uni.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: function() {
                    uni.showToast({
                      title: "文件已保存至/DCIM/CAMERA文件夹下",
                      icon: "none"
                    })
                    setTimeout(function() {
                      // 预览pdf文件
                      uni.openDocument({
                        filePath: res.tempFilePath,
                        showMenu: true,
                        success: function(file) {
                          console.log("file-success", file)
                        }
                      })
                    }, 1500)
                  },
                  fail: function() {
                    uni.showToast({
                      title: "保存失败,请稍后重试!",
                      icon: "none"
                    })
                  }
                })
              }
            }
          })
          // #endif
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
  • 相关阅读:
    JavaScript阻塞与非阻塞
    基于 ESP32-C2 的 Wi-Fi/BLE 和 LoRa 极低成本无线组网方案
    MySQL并发事务访问相同记录
    Yolov8和Yolov10的差异以及后处理实现
    【Hadoop】9、Sqoop组件
    pandas定位选取某列某指标最大值所在的行记录,比如月底
    Seata 环境搭建
    前端框架Vue学习 ——(七)Vue路由(Vue Router)
    基础数据结构之链表相关的一些问题
    强静态类型,真的无敌
  • 原文地址:https://blog.csdn.net/qq_42611074/article/details/126520598