微信小程序视频上传

1、wxml

 <video id='video{{idx}}' show-center-play-btn="{{true}}" src='{{videoUrl}}' controls="true" objectFit="cover">
  </video>
 <button class="start_btn"   bindtap="addVideoTap">添加视频</button>

2、js

Page({
  /**
   * 页面的初始数据
   */
  data: {
    videoUrl:''
  },
//点击上传视频按钮
  addVideoTap: function () {
    var that = this;
//选择上传视频
    wx.chooseVideo({
      sourceType: ['camera'], //视频选择的来源
      //sizeType: ['compressed'],
      compressed:false,//是否压缩上传视频
      camera: 'back', //默认拉起的是前置或者后置摄像头
      success: function (res) {
        //上传成功,设置选定视频的临时文件路径
        that.setData({
          videoUrl: res.tempFilePath
        });
        //在上传到服务器前显示加载中
        wx.showLoading({
          title: '加载中...',
          icon: 'loading',
        });
          //上传视频
          wx.uploadFile({
            url: '/upload/service/uploadFiles', //开发者服务器的 url
            filePath: res.tempFilePath, // 要上传文件资源的路径 String类型!!!
            name: 'file', // 文件对应的 key ,(后台接口规定的关于图片的请求参数)
            header: {
              'content-type': 'multipart/form-data'
            }, // 设置请求的 header
            formData: {

            }, // HTTP 请求中其他额外的参数
            success: function (res) {
              //上传成功后隐藏加载框
              wx.hideLoading(); 
              console.log(res);
            },
            fail: function (res) {
              console.log("图片上传失败" + res);
            }
          })      
      }
    })
  }  
  
});

猜你喜欢

转载自blog.csdn.net/LLL_liuhui/article/details/82991527