微信小程序雲開發-雲存儲-上傳單個視頻到雲存儲並顯示到頁面上


一、wxml文件

<!-- 上傳視頻到雲存儲 -->
<button bindtap="chooseVideo" type="primary" class="uploadImg">上傳單個視頻</button>
<view class="myImage">
<video src="{{videoUrl}}"></video>
</view>

二、wxss文件

.myImage{
  margin: 30rpx auto;
  text-align: center;
}
image{
  width: 420rpx;
  height: 300rpx;
}
.uploadImg{
  margin: 30rpx;
}

三、js文件

在page頁面內寫代碼

 //第一步:選擇要上傳的視頻
  chooseVideo(){
    let that = this
    wx.chooseVideo({
      sourceType: ['album','camera'],
      maxDuration: 60,
      camera: 'back',
      success(res) {
        //console.log(res.tempFilePath);
        console.log("----------",res.tempFilePath);
        wx.showLoading({
          title: '上傳中',
         })
      //調用uploadImg(tempFile)函數,實現圖片上傳功能
        that.uploadVideo(res.tempFilePath)
      }
    })
  },
  //第二步:上傳所選視頻到雲存儲
  uploadVideo(tempFile){
    console.log("要上傳視頻的臨時路徑",tempFile)
    let timestamp = (new Date()).valueOf()
    wx.cloud.uploadFile({
      cloudPath: +timestamp+'.mp4',  //雲存儲的路徑,開發者自定義
      filePath: tempFile,           // 文件路徑
    }).then(res => {
      console.log("上傳成功",res)
      wx.showToast({
        title: '上傳成功',
        icon:"success",
        duration:2000
      })
      let that = this
       setTimeout(function(){
        that.setData({
          videoUrl: res.fileID
         })
    },2000)
    })
    .catch(err => {
      console.log("上傳失敗",err);
    })
  }

四、最終效果

 

 


免責聲明!

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



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