编程语言
首页 > 编程语言> > 小程序下载PDF文件

小程序下载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)
    })
  },

 

标签:function,console,log,res,程序,imgurl,PDF,下载,wx
来源: https://www.cnblogs.com/xqschool/p/14254761.html