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

     

  • 相关阅读:
    显示文件本地文件夹
    Select Dependencies选择依赖项
    搜索小技巧
    783. Minimum Distance Between BST Nodes BST节点之间的最小距离
    5. Longest Palindromic Substring 最长的回文子串
    12. Integer to Roman 整数转罗马数字
    3. Longest Substring Without Repeating Characters 最长的子串不重复字符
    539. Minimum Time Difference 最小时差
    43. Multiply Strings 字符串相乘
    445. Add Two Numbers II 两个数字相加2
  • 原文地址:https://www.cnblogs.com/xqschool/p/14254761.html
Copyright © 2020-2023  润新知