微信小程序API接入网站后下载页面怎么写?

发布时间:2024-01-16 16:28:25 作者:超级管理员 来源:WEB 浏览量(235) 点赞(482)
摘要:经过3天的研究终于搞定了。。直接上代码:wxml:<buttondata-download="{{url}}"bindtap="downloadImg">下载</button>JS :downloadImg:function(e){wx.showLoading({title:"加载

经过3天的研究

终于搞定了。。直接上代码:

image.png

wxml:


<button data-download="{{url}}" bindtap="downloadImg">下载</button>


JS :

downloadImg: function (e) {
     wx.showLoading({
      title:"加载中" 
    });
    // wx.downloadFile方法:下载文件到本地
      wx.downloadFile({
      url:e.currentTarget.dataset.download,   
      success:function(res) {
        // wx.saveImageToPhotosAlbum保存图片到系统相册
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath, // 图片文件路径
          success:function(data) {
            wx.hideLoading() // 隐藏loading 提示框
            wx.showModal({
              title: '提示',
              content: '保存成功',
              modalType:false
            })
          },
          // 接口调用失败案例
          fail:function(err) {
            if(err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response"){
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                modalType: false,
                success:modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log("settingdata",settingdata)
                      if(settingdata.authSetting['scope.writePhotosAlbum']){
                        wx.showModal({
                          title: '提示',
                          content: '获取权限成功,再次点击图片即可保存',
                          modalType: false,
                        })
                      }
                    },
                  })
                }
              })
            }
          },
          complete(res) {
            wx.hideLoading() // 隐藏loading提示框
          }
        })
      }
    })
    
  }


方法二:

JS:

wx.downloadFile({
      url: e.currentTarget.dataset.download,
      success: function(res) {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function(res) {
            console.log('打开文档成功')
          }
        })
      }
    })


二维码

扫一扫,关注我们

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

我有话说:

评论记录:

快来抢沙发~

感兴趣吗?

欢迎联系我们,我们愿意为您解答任何有关网站疑难问题!