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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM