一、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); }) }
四、最終效果

