原生小程序下载功能(直接复制用就行了)

本文介绍了一种使用微信小程序API实现PDF文件下载并直接打开的方法。通过调用downloadFile接口下载文件,再利用openDocument接口实现PDF文件的预览,同时提供了错误处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用的时候直接调用就行了
 this.downloadAll(url)//url是下载文件的地址 
//下载方法
  downloadAll(url) {
    wx.showLoading({
      title: '下载中...',
    })
    wx.downloadFile({
      url: url,
      success: function (res) {
        if (res.statusCode == 200) {
          wx.hideLoading();
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            fileType: 'pdf',
            showMenu: true,
            success: function (res) {
              console.log(res, "66666");
              console.log('打开文档成功')
            }
          })
        }
      },
      fail: function (err) {
        console.log('fail')
        console.log(err)
        wx.showToast({
          icon: 'none',
          title: '下载文件失败'
        });
      }
    })
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值