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

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
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)
    })
  },

 

posted @   小小强学习网  阅读(1339)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2018-01-09 PHPExcel读取文件日期处理,含时分秒(Thinkphp)
点击右上角即可分享
微信分享提示