Vue+element ui 上传视频

直接上代码
(利用了阿里云浏览器直传方式)

        <el-form-item label="视频:" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action=""
            :http-request="handlePreview2"
            :data="uploadImgData"
            :show-file-list="false"
            :on-change="handlePreview"
            :before-upload="beforeAvatarUpload"
            :on-progress="uploadVideoProcess"
            :on-success="handleAvatarSuccess"
          >
            <video
              width="100%"
              v-if="form.videoUrl"
              controls="controls"
              :key="menuKey"
            >
              <source :src="form.videoUrl" type="video/mp4" />
            </video>
            <i
              v-else-if="!form.videoUrl"
              class="el-icon-plus avatar-uploader-icon"
            ></i>
          </el-upload>
        </el-form-item>

data:
	  uploadImgData: { busiName: 32 },
	  url: '',
methods: 
      async handlePreview2(param) {
        let file = param.file
        var formData = new FormData()
        const data = await fileUpload()
        let key = data.data.dir + file.uid + file.name
        formData.append('key', key)
        formData.append('policy', data.data.policy)
        formData.append('OSSAccessKeyId', data.data.accessId)
        formData.append('success_action_status', '200')
        formData.append('signature', data.data.signature)
        formData.append('file', file.raw)
        let config = {
          method: 'post',
          url: data.data.host,
          Headers: {
            Accept: '*/*',
            'content-type': 'application/form-data',
          },
          data: formData,
          // 获取上传的进度
          onUploadProgress: (event) => {
            param.file.percent = (event.loaded / event.total) * 100
            this.url = data.data.host + '/' + key
            //此处调用处理中
            param.onProgress(param.file)
          },
        }
        axios(config).then((res) => {
          param.onSuccess(res)
        })
        if (data.success) {
          this.url = data.data.host + '/' + key
        } else {
          this.$message.error(data.message)
        }
      },
    async handlePreview(file) {
        var formData = new FormData()
        const data = await fileUpload()
        let key = data.data.dir + file.uid + file.name
        formData.append('key', key)
        formData.append('policy', data.data.policy)
        formData.append('OSSAccessKeyId', data.data.accessId)
        formData.append('success_action_status', '200')
        formData.append('signature', data.data.signature)
        formData.append('file', file.raw)
        let config = {
          method: 'post',
          url: data.data.host,
          Headers: {
            Accept: '*/*',
            'content-type': 'application/form-data',
          },
          data: formData,
        }
        axios(config)
        if (data.success) {
          this.form.videoUrl = data.data.filePath
          this.form.videoUrl = data.data.host + '/' + key
        } else {
          this.$message.error(data.message)
        }

        this.times = []
      },
      beforeAvatarUpload(file) {
        const isMp4 = file.type === 'video/mp4'
        // 限制文件最大不能超过 300M
        const isLt2M = file.size / 1024 / 1024 < 300

        if (!isMp4) {
          this.$message.error('视频只能是mp4格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传视频大小不能超过 300MB!')
        }
        return isMp4 && isLt2M
      },

效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值