微信小程序之----视频上传

1. 选择视频 (wx.chooseVideo()的使用方法)

官网链接地址: https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseVideo.html.

2. 上传视频大小限制处理
  if (res.size > 1024 * 1024 * 10) {
        wx.showToast({
          title: "上传视频不能超过10M!",
          icon: 'none',
        })
 }
3. 调用 wx.uploadFile(),将本地资源上传到服务器

官网链接地址: https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html.

4. 保存到数据库

以下是完整代码

//  上传视频
uploadVideo(){
  var that=this
  wx.chooseVideo({
    sourceType: ['album','camera'],
    maxDuration: 60,
    compressed:true,
    camera: 'back',
    success(res) {
      if (res.size > 1024 * 1024 * 10) {
        wx.showToast({
          title: "上传视频不能超过10M!",
          icon: 'none',
        })
        return;
      }else{
        var duration=res.duration
      that.setData({
        isShowProgress:true
      })
      wx.showLoading({
        title: '上传中...',
      })
      wx.uploadFile({
        url:uploadAudio.uploadAudio,
        filePath:res.tempFilePath,
        name: 'file',
        success(res) {
         var filePath = JSON.parse(res.data).data[0]
          var params = {
            userId:that.data.userId,
            url:filePath,
            duration:duration,
            type:3
          }
          postRecording(params).then(res=>{
            wx.hideLoading()
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 2000
            })
            that.setData({
              isShowProgress:false
            })
            that.init()
          })
        }
      })
      }
    },fail(){
      this.setData({
        isShowProgress:false
      })
      wx.hideLoading()
      wx.showToast({
        title: '上传失败',
        icon: 'none',
        duration: 1000
      })
    }
  })
},
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值