一区在线免费观看-一区在线免费-一区在线看-一区免费在线观看-日韩欧美网站

微信小程序API接入網(wǎng)站后下載頁面怎么寫?

發(fā)布時間:2024-01-16 16:28:25 作者:超級管理員 來源:WEB 瀏覽量(212) 點(diǎn)贊(467)
摘要:經(jīng)過3天的研究終于搞定了。。直接上代碼:wxml:<buttondata-download="{{url}}"bindtap="downloadImg">下載</button>JS :downloadImg:function(e){wx.showLoading({title:"加載

經(jīng)過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保存圖片到系統(tǒng)相冊
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath, // 圖片文件路徑
          success:function(data) {
            wx.hideLoading() // 隱藏loading 提示框
            wx.showModal({
              title: '提示',
              content: '保存成功',
              modalType:false
            })
          },
          // 接口調(diào)用失敗案例
          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: '需要您授權(quán)保存相冊',
                modalType: false,
                success:modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log("settingdata",settingdata)
                      if(settingdata.authSetting['scope.writePhotosAlbum']){
                        wx.showModal({
                          title: '提示',
                          content: '獲取權(quán)限成功,再次點(diǎn)擊圖片即可保存',
                          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('打開文檔成功')
          }
        })
      }
    })


二維碼

掃一掃,關(guān)注我們

打賞

取消

感謝您的支持,我會繼續(xù)努力的!

掃碼支持
掃碼打賞,你說多少就多少

打開支付寶掃一掃,即可進(jìn)行掃碼打賞哦

我有話說:

評論記錄:

快來搶沙發(fā)~

感興趣嗎?

歡迎聯(lián)系我們,我們愿意為您解答任何有關(guān)網(wǎng)站疑難問題!