VUE 基于 element 上傳視頻,顯示進(jìn)度條

<template>
  <div class="upload-video">
    <div id="videoBox" ref="videoBox"></div>
    <div>
      <el-upload
        v-loading="uploadLoading"
        class="upload-demo"
        :action="''"
        :multiple="false"
        accept="video/mp4,video/quicktime,video/avi"
        :auto-upload="false"
        :file-list="fileList"
        :limit="1"
        :on-change="handleVideoChange"
        :on-remove="handleRemove"
        :on-exceed="handleExceed"
        :on-error="handleError"
      >
        <el-button plain>上傳視頻</el-button>
        <div slot="tip" class="el-upload__tip">
          選擇本地視頻上傳,不支持多條視頻同步上傳
        </div>
      </el-upload>
    </div>
    <div v-if="progressSeen">
      <el-progress
        v-if="!uploadStatus"
        :percentage="progress"
        status="warning"
        style="width:350px"
      ></el-progress>
      <el-progress
        v-else-if="uploadStatus && progress !== 100"
        :percentage="progress"
        style="width:350px"
      ></el-progress>
      <el-progress v-else :percentage="progress" status="success" style="width:350px"></el-progress>
    </div>
    <!-- <el-button plain @click="test">測試</el-button> -->
  </div>
</template>

<script>
export default {
  name: 'UploadVideo',
  props: {
    content: {
      type: String,
      default: ''
    },
    uploadImgMaxSize: {
      type: Number,
      default: 10 * 1024 * 1024
    },
    uploadImgServer: {
      type: String,
      default: ''
    },
    uploadFileName: {
      type: String,
      default: 'file'
    },
    uploadImgAccept: {
      type: Array,
      default: () => ['jpg', 'jpeg', 'png', 'gif']
    }
  },
  data () {
    return {
      uploadSign: {
        host: ''
      },
      uploadLoading: false,
      fileList: [],
      progress: 0,
      progressSeen: false,
      uploadStatus: true
    }
  },
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    async handleVideoChange (file) {
      const validFile = file.raw
      //只能插入一個(gè)視頻
      console.log(validFile)
      if (this.fileList.length > 0) {
        this.$message.error('只能插入一個(gè)視頻')
        return false
      }
      const isLt1G = validFile && validFile.size / 1024 / 1024 / 1024 < 1
      if (!isLt1G) {
        this.$message.error('視頻大小不能超過1G')
        return false
      }
      this.uploadStatus = true
      this.progressSeen = true
      this.progress = 10
      await this.getSignature(validFile && validFile.name)
      const client = {
        key: this.uploadSign.filePath,
        policy: this.uploadSign.policy,
        OSSAccessKeyId: this.uploadSign.accessId,
        signature: this.uploadSign.signature,
        expire: this.uploadSign.expire,
        dir: this.uploadSign.dir,
        callBack: this.uploadSign.callBack,
        success_action_status: '200'
      }
      this.uploadOssVideo(client, validFile)
    },
    async getSignature (fileName) {
      let res = await fetchApi.article.getUploadSign({ fileName })
      this.uploadSign = res.data
      console.log(this.uploadSign)
    },
    async uploadOssVideo (client, file) {
      let _this = this
      var oMyForm = new FormData()

      for (var field_name in client) {
        oMyForm.append(field_name, client[field_name])
      }

      oMyForm.append('file', file)

      var oReq = new XMLHttpRequest()
      //上傳進(jìn)度監(jiān)聽
      oReq.upload.onprogress = function (e) {
        if (e.type == 'progress') {
          _this.uploadLoading = true
          var percent = Math.round((e.loaded / e.total) * 100, 2)
          _this.progress = percent
        }
      }
      //上傳結(jié)果
      oReq.onreadystatechange = function (e) {
        _this.uploadLoading = false
        if (oReq.readyState == 4) {
          if (oReq.status == 200) {
            //這里如果成功返回的是 success_action_status設(shè)置的值
            _this.$message.success('上傳成功')
            try {
              const path = e.target.response && JSON.parse(e.target.response).fileFullPath
              console.log(path)
              _this.$emit('uploadVide', path)
            } catch (error) {}
          } else {
            _this.$message.error('上傳失敗')
            this.uploadStatus = false
          }
        }
      }
      oReq.open('POST', this.uploadSign.host)
      oReq.send(oMyForm)
    },

    test () {
      this.handleError()
    },
    handleRemove (file, fileList) {
      this.progressSeen = false
      this.progress = 0
    },
    handleExceed () {
      this.$message.warning(`只能插入一個(gè)視頻`)
    },
    handleError () {
      this.$message.error('上傳失敗')
      this.uploadStatus = false
      // this.fileList = []
      this.$emit('uploadVide', '')
    }
  }
}
</script>

<style lang="scss">
.upload-video {
  .el-upload-list__item-name {
    padding-left: 0px !important;
  }
}
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末照卦,一起剝皮案震驚了整個(gè)濱河市砰粹,隨后出現(xiàn)的幾起案子么介,更是在濱河造成了極大的恐慌医舆,老刑警劉巖拌汇,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蚯根,死亡現(xiàn)場離奇詭異驹止,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)凶赁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門咧栗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來逆甜,“玉大人,你說我怎么就攤上這事致板〗簧罚” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵斟或,是天一觀的道長素征。 經(jīng)常有香客問我,道長萝挤,這世上最難降的妖魔是什么御毅? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮怜珍,結(jié)果婚禮上端蛆,老公的妹妹穿的比我還像新娘。我一直安慰自己酥泛,他們只是感情好今豆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著揭璃,像睡著了一般晚凿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上瘦馍,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機(jī)與錄音应役,去河邊找鬼情组。 笑死,一個(gè)胖子當(dāng)著我的面吹牛箩祥,可吹牛的內(nèi)容都是我干的院崇。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼袍祖,長吁一口氣:“原來是場噩夢啊……” “哼底瓣!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蕉陋,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤捐凭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后凳鬓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體茁肠,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年缩举,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了垦梆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片匹颤。...
    茶點(diǎn)故事閱讀 40,102評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖托猩,靈堂內(nèi)的尸體忽然破棺而出印蓖,到底是詐尸還是另有隱情,我是刑警寧澤京腥,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布赦肃,位于F島的核電站,受9級特大地震影響绞旅,放射性物質(zhì)發(fā)生泄漏摆尝。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一因悲、第九天 我趴在偏房一處隱蔽的房頂上張望堕汞。 院中可真熱鬧,春花似錦晃琳、人聲如沸讯检。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽人灼。三九已至,卻和暖如春顾翼,著一層夾襖步出監(jiān)牢的瞬間投放,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工适贸, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留灸芳,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓拜姿,卻偏偏與公主長得像烙样,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子蕊肥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容