微信小程序视频、图片、上传

307 阅读1分钟

大家好,这一期讲一下微信原生小程序的一些 api

1.微信小程序视频上传

  // 开始录制视频
  startVideo() {
    let _this = this
    wx.chooseMedia({
      sourceType: ['album', 'camera'],
      maxDuration: 30, // 时间限制
      camera: 'back',
      success(res) {
        console.log(res.tempFiles[0].tempFilePath, '视频成功的地址')
        _this.setData({
          videoSrc: res.tempFiles[0].tempFilePath
        })
      }
    })
  },

2.文件图片上传

  // 图片上传
  chooseImage() {
    wx.chooseMedia({
      count: 1, // 可选择的图片数量
      sizeType: ['compressed'], // 压缩图片
      sourceType: ['album', 'camera'], // 来源:相册或相机
      success: (res) => {
        let arr = []
        // 将选择的图片上传到服务器
        this.uploadImage(res.tempFilePaths[0],'成功后的路径地址');
      }
    })
  },

3.预览图片

  previewImage: function (e) {
    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: imglist, // 需要预览的图片http链接列表
    })
  },