uni-app笔记 App端文件上传

由于uni.chooseFile api不支持app端,所以使用h5+的api来实现

网址如下:

h5+方法网址如下

获取摄像头对象 HTML5+ API Reference

获取相册 HTML5+ API Reference

 //在mounted中新建摄像头对象
   mounted() {
    this.camera = plus.camera.getCamera() //获取摄像头管理对象
  }
  methods: {
     handleUploadType(type) {
      // 1 拍照 2拍视频 3选择照片 4 选择视频
      //下方上传成功的res返回的都是文件路径,拿到上传的文件路径传给后端拿到自己想要的信息
      let _this = this
      switch (type) {
        case 1:
          _this.camera.captureImage(function (res) {
            _this.uploadFileImageSuccess(res)
          })
          break
        case 2:
          _this.camera.startVideoCapture(function (res) {
            _this.uploadFileVideoSuccess(res)
          })
          break
        case 3:
          plus.gallery.pick(
            function (res) {
              _this.uploadFileImageSuccess(res)
            },
            function (e) {
              console.log('取消选择图片', e)
            },
            { filter: 'image' }
          )
          break
        case 4:
          plus.gallery.pick(
            function (res) {
              console.log('选择的视频', res)
              _this.uploadFileVideoSuccess(res)
            },
            function (e) {
              console.log('取消选择视频', e)
            },
            { filter: 'video' }
          )
          break
      }
      _this.changeOverlay = false
    },
  }
  uploadFileVideoSuccess(res) {
      let _this = this
      _this.upLoading = true
      uploadFile(
        {
          url: '/infra/file/upload',
          filePath: res,
          name: 'file',
          formData: {
            fileType: 4,
            businessCode: 6,
          },
        },
        false
      )
        .then((data) => {
          _this.upLoading = true
          this.uploadList.push(data)
          this.videoType = true
          //获取拿到的视频的详细信息
          uni.getVideoInfo({
            src: res,
            success: function (res) {
              data.duration = _this.convertSecondsToTime(res.duration)
              uni.getImageInfo({
                src: data.videoPicPath,
                success: function (image) {
                  _this.uploadVideoForm.snapshotHeight = image.height
                  _this.uploadVideoForm.snapshotWidth = image.width
                  _this.uploadVideoForm.snapshotSize = null
                },
              })
            },
            fail: function (err) {
              console.log('获取失败', err)
            },
          })
        })
        .finally(() => {
          _this.upLoading = false
        })
    },

    uploadFileImageSuccess(res) {
      let _this = this
      _this.upLoading = true
      uploadFile(
        {
          url: '/infra/file/upload',
          filePath: res,
          name: 'file',
          formData: {},
        },
        false
      )
        .then((data) => {
          _this.upLoading = true
          this.uploadList.push(data)
        })
        .finally(() => {
          _this.upLoading = false
        })
    },

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值