微信小程序雲開發-雲存儲-上傳文件(word/excel/ppt/pdf)到雲存儲


說明 word/excel/ppt/pdf是從客戶端會話選擇文件。使用chooseMessageFile中選擇文件。

一、wxml文件

上傳按鈕,綁定chooseFile

<!--上傳文件(word/excel/ppt/pdf等)到雲存儲-->
<button bindtap="chooseFile" type="primary" >上傳文件</button>

二、wxss文件

wxss設置按鈕外邊距

button{
  margin: 30rpx;
}

三、js文件

實現文件選擇和文件上傳的功能

Page({
  //功能:上傳文件(word/excel/ppt/pdf等)到雲存儲

  //第一步:選擇文件
  chooseFile(){
    let that = this
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success (res) {
        // tempFilePath可以作為img標簽的src屬性顯示圖片
        const tempFilePaths = res.tempFiles
        let tempFile = tempFilePaths[0]
        that.uploadFile(tempFile.name,tempFile.path)
      }
    })
  },
  //第二步:通過uploadFile上傳選中的文件
  uploadFile(fileName,tempFile){
    wx.cloud.uploadFile({
      cloudPath:fileName,
      filePath:tempFile,
    })
  .then(res=>{
    console.log("上傳成功啦",res);
    wx.showToast({
      title: '文件上傳成功',
      icon:"success",
      duration:2000
    })
  })
  .catch(err=>{
    console.log("上傳失敗啦",err);
  })
  }
  
})

四、效果展示

 


免責聲明!

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



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