zoukankan      html  css  js  c++  java
  • 小程序下载PDF文件

    1:直接打开文件

     savePicture: function(e) {
        var _this=this;
        console.log(e);
        var url=e.currentTarget.dataset.url;
        url="https://chengfei.dev145.ynccxx.net/web/a.doc";
        wx.getSetting({
          success: function (t) {
            var imgurl = url;
            if (imgurl.indexOf('https://') === -1) imgurl = imgurl.replace('http://', 'https://');
            t.authSetting["scope.writePhotosAlbum"] ? (wx.showLoading({
              title: "下载中"
            }), setTimeout(function () {
              wx.hideLoading()
            },
              1e3), wx.downloadFile({
                 url: imgurl,
                success: function (t) {
                  console.log(t);
                  console.log(t.tempFilePath);
                  wx.openDocument({
                    filePath: t.tempFilePath,
                    success: function (t) {
                      console.log(t);
                      console.log("222222")
                    },
                    fail: function (t) {
                      console.log(t);
                      console.log("3333333333")
                    }
                  })
                }
              })) : wx.authorize({
                scope: "scope.writePhotosAlbum",
                fail: function () {
                  wx.showModal({
                    title: "警告",
                    content: "您点击了拒绝授权,将无法正常使用保存图片或视频的功能体验,请删除小程序重新进入。"
                  })
                }
              })
          }
        })
      },

    2:下载保存PDF

     savePicture: function(e) {
        var _this=this;
        console.log(e);
        var url=_this.data.fileurl;
        wx.getSetting({
          success: function (t) {
            var imgurl = url;
            if (imgurl.indexOf('https://') === -1) imgurl = imgurl.replace('http://', 'https://');
            t.authSetting["scope.writePhotosAlbum"] ? (wx.showLoading({
              title: "下载中请稍后"
            }), setTimeout(function () {
              wx.hideLoading()
            },
              1e3), _this.download()) : wx.authorize({
                scope: "scope.writePhotosAlbum",
                fail: function () {
                  wx.showModal({
                    title: "警告",
                    content: "您点击了拒绝授权,将无法正常使用保存图片或视频的功能体验,请删除小程序重新进入。"
                  })
                }
              })
          }
        })
      }
    
    download:function(){
        let _this=this;
        let f=this.data.fileurl;
        const task=wx.downloadFile({
          url: f,
          success:function(res){
            console.log(44,res);
            const t=res.tempFilePath;
            wx.saveFile({
              tempFilePath: t,
              success:function(res){
                wx.openDocument({
                  showMenu:true,
                  filePath: res.savedFilePath,
                  success:function(res){
                    
                  },
                  fail:function(r){
                    wx.showToast({
                      title: '打开文件失败',
                    })
                  }
                })
                wx.showToast({
                  title: '下载成功',
                })
              },
              fail:function(r){
                console.log(67,r)
              }
            })
          },
          fail:function(r){
            console.log(72,r);
          }
        })
        task.onProgressUpdate((res) => {
          if(res.progress<100){
            _this.setData({
              downloadproce:'下载中('+res.progress+'%)'
            })
          }else{
            _this.setData({
              downloadproce:'资料下载'
            })
          }
          
          console.log('下载进度', res.progress)
          console.log('已经下载的数据长度', res.totalBytesWritten)
          console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
        })
      },
    

     

  • 相关阅读:
    TimeVal类——Live555源码阅读(一)基本组件类
    QT国际化 一 (lupdate/linguits/lrelease)
    qmake的使用
    将Apache加入到linux系统service
    ubuntu下编译VLC
    Ubuntu14.04安装intel集显驱动
    net-snmp的dateandtime数据类型
    MySQL的主从复制原理
    【iOS开发-25】UIDevice查看系统信息,从一个问题開始怎样高速找到自己想要的属性和方法并看懂它
    POJ 3279 Fliptile (二进制+搜索)
  • 原文地址:https://www.cnblogs.com/xqschool/p/14254761.html
Copyright © 2011-2022 走看看