小程序 ------- 文件下載


 wx.downloadFile :下載文件資源到本地。客戶端直接發起一個 HTTPS GET 請求,返回文件的本地臨時路徑 (本地路徑),單次下載允許的最大文件為 50MB。使用前請注意閱

注意:請在服務端響應的 header 中指定合理的 Content-Type 字段,以保證客戶端正確處理文件類型。

wx.saveImageToPhotosAlbum :保存圖片到系統相冊。
 wx.saveFile :保存文件到本地。注意:saveFile 會把臨時文件移動,因此調用成功后傳入的 tempFilePath 將不可用
 wx.openDocument:新開頁面打開文檔。微信客戶端  7.0.12 版本前默認顯示右上角菜單按鈕,之后的版本默認不顯示,需主動傳入  showMenu
handlerdownFile(evt){
   // console.log(evt.currentTarget.dataset)
    const filePathName = evt.currentTarget.dataset.name;

      //console.log(this.data.arrStudyZiLiao[0].Ext02)
  // let filePathNameLenght = filePathName.length;
   // console.log(filePathNameLenght)
    let filePathNameHZ = filePathName.lastIndexOf('.')
  //  console.log(filePathNameHZ)
    let houZhui =  filePathName.substring(filePathNameHZ+1).toLowerCase();
   // console.log(houZhui)
    const imgType = "|png|gif|jpg|";
    wx.downloadFile({
      url: 'https://www.obge.com/UpLoad/Fujian/'+filePathName,
      success: function(res){
        //console.log(res);
        const tempPath=res.tempFilePath;

        if(imgType.indexOf(houZhui)>0){
          wx.saveImageToPhotosAlbum({      
            filePath:tempPath,
            success(res) {
              wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
              })
            }
          })
        }else{
          //保存預覽文件
          wx.saveFile({
            tempFilePath: tempPath, 
            success:function(res){
              const savePath = res.savedFilePath;
              wx.openDocument({
                filePath: savePath,
                success :function(res){
                 // console.log(res);
                  console.log('打開文檔成功')
                },
              });
            },
            fail: function (err) {
              console.log('保存失敗:', err)
            }
          })
    
        }

      },
      fail: function (err) {
        console.log('下載失敗:', err);
      },

    })
},

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM