小程序上传图片和视频代码实例

js页面
  // 拍照
  camera: function(that) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ["camera"],
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.uploadFile(tempFilePaths);
      },
    })
  },
  // 从相册选取
  album: function(that) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ["album"],
      success: function(res) {
        const tempFilePaths = res.tempFilePaths
        that.uploadFile(tempFilePaths);
      },
    })
  },
  // 拍摄视频
  video: function(that) {
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      compressed: '',
      maxDuration: 60,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.uploadFile(tempFilePaths);
      },
    })
  },
  // 上传
  uploadFile: function(filePath) {
    wx.uploadFile({
      url: '',
      filePath: filePath,
      name: '',
      header: {},
      formData: {},
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }`

你可能感兴趣的:(知识分享)