欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue vantUI實(shí)現(xiàn)文件(圖片、文檔、視頻、音頻)上傳(多文件)

 更新時(shí)間:2019年10月15日 10:27:46   作者:Nanana  
這篇文章主要介紹了vue vantUI實(shí)現(xiàn)文件(圖片、文檔、視頻、音頻)上傳(多文件),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

說在前面

網(wǎng)上有很多種文件上傳的方法,根據(jù)公司最近的業(yè)務(wù)需求,要求實(shí)現(xiàn)多種文件的上傳,在實(shí)現(xiàn)過程中,查閱了很多資料,最后,終于把功能實(shí)現(xiàn)了,開心!

<template>
  <div>
    <v-header :left-text="`上傳${columnName}`"></v-header>

    <div class="upload">
      <div v-if="columnName === '視頻'">
        <div class="forPreview_video" v-for="(item, index) in uploadVideoList" :key="index">
          <video :src="videoSrc"></video>
          <van-icon name="delete" @click="delBtn(index)" class="delte"/>
        </div>
        <van-uploader v-show="uploadVideoList.length < 2" preview-size="80px" accept="video/*" :before-read="beforeRead" :after-read="afterRead"></van-uploader>
      </div>

      <div v-if="columnName === '文檔'">
        <div class="forPreview_doc" v-for="(item, index) in uploadDocList" :key="index">
          <img src="../../assets/img/resource_doc_b@2x.png" alt="">
          <span>{{item.name}}</span>
          <span>{{item.size | formatSize}}</span>
          <van-icon name="delete" @click="delBtn(index)" class="delte"/>
        </div>
        <van-uploader v-show="uploadDocList.length < 2" preview-size="80px" accept=".doc, .docx, .xml, .xlsx, .pdf" :before-read="beforeRead" :after-read="afterRead"></van-uploader>
      </div>

      <div v-if="columnName === '音頻'">
        <div class="forPreview_audio" v-for="(item, index) in uploadAudioList" :key="index">
          <img src="../../assets/img/resource_audio@2x.png" alt="">
          <span>{{item.name}}</span>
          <span>{{item.size | formatSize}}</span>
          <van-icon name="delete" @click="delBtn(index)" class="delte"/>
        </div>
        <van-uploader v-show="uploadAudioList.length < 2" preview-size="80px" accept="audio/*" :before-read="beforeRead" :after-read="afterRead"></van-uploader>
      </div>

      <div v-if="columnName === '圖片'">
        <div class="forPreview_pic" v-for="(item, index) in uploadPicList" :key="index">
          <img :src="picSrc" alt="">
          <van-icon name="delete" @click="delBtn(index)" class="delte"/>
        </div>
        <van-uploader v-show="uploadPicList.length < 2" accept="image/*" preview-size="80px" :before-read="beforeRead" :after-read="afterRead"></van-uploader>
      </div>

      <div class="diy-submit">
        <van-button @click="doSubmit($event)">提交</van-button>
      </div>
    </div>
  </div>
</template>

上述html代碼是在同一頁面根據(jù)前一頁面?zhèn)骰氐腸olumnName的值來判斷需要上傳哪種類型文件。

<script>
  import VHeader from '../../common/header'
  import {classifyList, uploadFile, addResources} from '../../http/mock'
  import Toast from 'vant'
  export default {
    name: "uploadFile",
    components: {
      VHeader
    },
    data(){
      return{
        tagList: [], 
        uploadTitle: '',
        currentTag: null,
        tagId: null,
        columnName: localStorage.getItem('columnName'),
        fileIdArr: [],

        uploadVideoList: [], // 選中的上傳視頻列表
        videoSrc: '',  // 選中的視頻的src,用來顯示視頻
        uploadDocList: [], // 選中的上傳文檔列表
        uploadAudioList: [], // 選中的上傳音頻列表
        uploadPicList: [], // 選中的上傳圖片列表
        picSrc: '', // 選中的圖片的src,用來顯示圖片縮略圖
      }
    },
    filters: {
      formatSize(val) { // 格式化文件大小
        if (val > 0) {
          return (val / 1024 / 1024).toFixed(2) + 'M';
        } else {
          return '0M';
        }
      },
    },
    methods: {
      // vant上傳文件前校驗(yàn)文件事件
      // 文件選中后先提交給后臺(tái),后臺(tái)根據(jù)選中的文件,返回?cái)?shù)組(這一業(yè)務(wù)根據(jù)后臺(tái)而定)
      beforeRead(file){
        let formData = new FormData(); // 為上傳文件定義一個(gè)formData對(duì)象
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        };
        let uploadUrl = URL.createObjectURL(file); // 將選中的上傳文件轉(zhuǎn)化為二進(jìn)制文件,顯示在頁面上
        if(this.columnName === '視頻'){
          this.uploadVideoList.push(file);
          this.videoSrc = uploadUrl;
          // 這里使用foreach是為了將選中的多個(gè)文件都添加進(jìn)定義的formdata變量中
          this.uploadVideoList.forEach(item => {
            formData.append(item.name, item)
          })
        }else if(this.columnName === '文檔'){
          this.uploadDocList.push(file);
          this.uploadDocList.forEach(item => {
            formData.append(item.name, item)
          })
        }else if(this.columnName === '音頻'){
          this.uploadAudioList.push(file);
          this.uploadAudioList.forEach(item => {
            formData.append(item.name, item)
          })
        }else if(this.columnName === '圖片'){
          this.uploadPicList.push(file);
          this.picSrc = uploadUrl;
          this.uploadPicList.forEach(item => {
            formData.append(item.name, item)
          })
        }


        // formData.append(file.name, file); // 單個(gè)文件上傳時(shí)可以這么寫,上面的foreach就可以刪掉
        this.$api.post(uploadFile, formData, config).then(res => {
          this.fileIdArr = res.data.data;  // 把選中的文件傳送給後臺(tái)
        }).catch(err => {
          Toast('文件上傳失??!')
        })
      },
      // 刪除待上傳的文件
      delBtn(index){
        if(this.columnName === '視頻'){
          // 先判斷當(dāng)前的選中的索引是否是在有效范圍中,如果不是則跳出方法 
          if(isNaN(index) || index >= this.uploadVideoList.length){
            return false;
          }
          let tmp = [];
          // 將沒被選中的上傳文件存放進(jìn)一個(gè)臨時(shí)數(shù)組中
          for(let i = 0; i < this.uploadVideoList.length; i++){
            if(this.uploadVideoList[i] !== this.uploadVideoList[index]){
              tmp.push(this.uploadVideoList[i]);
            }
          }
          // 存放當(dāng)前未被選中的上傳文件
          this.uploadVideoList = tmp;
        }

        if(this.columnName === '文檔'){
          console.log(this.uploadDocList.length)
          if(isNaN(index) || index >= this.uploadDocList.length){
            return false;
          }
          let tmp = [];
          for(let i = 0; i < this.uploadDocList.length; i++){
            if(this.uploadDocList[i] !== this.uploadDocList[index]){
              tmp.push(this.uploadDocList[i]);
            }
          }
          this.uploadDocList = tmp;
        }

        if(this.columnName === '音頻'){
          if(isNaN(index) || index >= this.uploadAudioList.length){
            return false;
          }
          let tmp = [];
          for(let i = 0; i < this.uploadAudioList.length; i++){
            if(this.uploadAudioList[i] !== this.uploadAudioList[index]){
              tmp.push(this.uploadAudioList[i]);
            }
          }
          this.uploadAudioList = tmp;
        }

        if(this.columnName === '圖片'){
          if(isNaN(index) || index >= this.uploadPicList.length){
            return false;
          }
          console.log('uuu')
          let tmp = [];
          for(let i = 0; i < this.uploadPicList.length; i++){
            if(this.uploadPicList[i] !== this.uploadPicList[index]){
              tmp.push(this.uploadPicList[i]);
            }
          }
          this.uploadPicList = tmp;
        }
      },
      doSubmit(){
        let params = {
          classify: this.tagId,    // 針對(duì)視頻資源時(shí)對(duì)應(yīng)的分類id
          file: this.fileIdArr,  // 選擇完文件后,調(diào)用uploadFile這個(gè)接口,后端返回的數(shù)組
          resourceColumnId: JSON.parse(localStorage.getItem('columnId')),  // 資源欄目id(視頻、圖片、音頻、文檔)
          title: this.uploadTitle  // 上傳時(shí)填寫的標(biāo)題
        };
        let columnName = localStorage.getItem('columnName')
        this.$api.post(addResources, params).then(res => {
          if(res.data.code === 1001){
            if(columnName === '視頻'){
              this.$router.push({name: 'myVideo'});
            }else {
              this.$router.push({name: 'myResourceClassify'});
            }
          }
        }).catch(err => {
          console.log(err)
        })
      },
    },
    mounted(){
      this.getClassifyList();
    }
  }
</script>
<style lang="less" scoped>
  .upload{
    padding: 140px 36px 160px 36px;
    box-sizing: border-box;
  }

  .forPreview_video{
    position: relative;
    /*background: rgba(0,0,0,.7);*/
    video{
      width: 95%;
      max-height: 430px;
    }
    .delte{
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
  .forPreview_doc, .forPreview_audio{
    display: flex;
    margin-bottom: 10px;
    align-items: center;
    img{
      width: 56px;
      height: 56px;
      margin-right: 20px;
    }
    span:nth-of-type(1){
      flex: 1;
    }
    span:nth-of-type(2){
      margin-right: 20px;
    }
  }
  .forPreview_pic{
    display: flex;
    align-items: flex-end;
    img{
      width: 160px;
      height: 160px;
    }
  }

  .diy-detail{
    width: 100%;
    overflow: hidden;
    
    .btn{
      span{
        margin-bottom: 10px;
      }
    }
    .van-cell{
      background-color: #F0F0F0;
      border-radius: 35px;
      font-size: 26px;
      height: 69px;
      line-height: 69px;
      padding: 0 22px;
      color: #999;
    }
    .van-hairline--top-bottom::after, .van-hairline-unset--top-bottom::after {
      border-width: 0;
    }
    p{
      height: 64px;
      line-height: 64px;
      font-size: 32px;
      color: #333;
      position: relative;
      padding-left: 16px;
    }
    p::before{
      position: absolute;
      top: 0;
      left: 0;
      content: '*';
      color: #FF0000;
    }

    span{
      display: inline-block;
      width: 157px;
      background: #F0F0F0;
      border-radius: 35px;
      color: #999;
      font-size: 26px;
      padding: 14px 18px;
      margin-right: 28px;
      text-align: center;
    }
    .active{
      color: #fff;
      background: linear-gradient(to right,#FD5130,#FA6C34);
    }
  }
  .diy-submit {
    position: fixed;
    height: 150px;
    width: 90%;
    bottom: 0;
    background: #fff;

    .van-button {
      width: 100%;
      height: 90px;
      border-radius: 45px;
      font-size: 36px;
      color: #fff;
      background: linear-gradient(to right, #FD5130, #FA6C34);
      top: 50%;
      transform: translate(0, -50%);
    }
    .van-button--default {
      border: none;
    }
  }
</style>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Vue中使用Axios攔截器

    詳解Vue中使用Axios攔截器

    這篇文章主要Vue使用Axios攔截器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Vue package.json配置深入分析

    Vue package.json配置深入分析

    這篇文章主要介紹了Vue package.json配置,package.json是每個(gè)前端項(xiàng)目都會(huì)有的json文件,位于項(xiàng)目的根目錄中。很多腳手架在創(chuàng)建項(xiàng)目的時(shí)候會(huì)幫我們自動(dòng)初始化好 package.json
    2023-01-01
  • Vue超詳細(xì)講解重試機(jī)制示例

    Vue超詳細(xì)講解重試機(jī)制示例

    這篇文章主要介紹了Vue重試機(jī)制示例,重試指的是當(dāng)加載出錯(cuò)時(shí),有能力重新發(fā)起加載組件的請(qǐng)求。異步組件加載失敗后的重試機(jī)制,與請(qǐng)求服務(wù)端接口失敗后的重試機(jī)制一樣
    2023-01-01
  • vue中的路由跳轉(zhuǎn)tabBar圖片和文字的高亮效果

    vue中的路由跳轉(zhuǎn)tabBar圖片和文字的高亮效果

    這篇文章主要介紹了vue中的路由跳轉(zhuǎn)tabBar圖片和文字的高亮效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 解決vue語法會(huì)有延遲加載顯現(xiàn){{xxx}}的問題

    解決vue語法會(huì)有延遲加載顯現(xiàn){{xxx}}的問題

    今天小編就為大家分享一篇解決vue語法會(huì)有延遲加載顯現(xiàn){{xxx}}的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Vue組件實(shí)例間的直接訪問實(shí)現(xiàn)代碼

    Vue組件實(shí)例間的直接訪問實(shí)現(xiàn)代碼

    在組件實(shí)例中,Vue提供了相應(yīng)的屬性,包括$parent、$children、$refs和$root,這些屬性都掛載在組件的this上。本文將詳細(xì)介紹Vue組件實(shí)例間的直接訪問,需要的朋友可以參考下
    2017-08-08
  • vue3.x使用swiperUI動(dòng)態(tài)加載圖片失敗的解決方法

    vue3.x使用swiperUI動(dòng)態(tài)加載圖片失敗的解決方法

    這篇文章主要為大家詳細(xì)介紹了vue3.x使用swiperUI動(dòng)態(tài)加載圖片失敗的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • iphone劉海屏頁面適配方法

    iphone劉海屏頁面適配方法

    這篇文章主要介紹了iphone劉海屏頁面適配方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Vue2 的12種組件通訊

    Vue2 的12種組件通訊

    之前文章我們描述給過Vue3 的七通信使用,今天中五篇文章我們?cè)賮砜纯碫ue2 的通信使用寫法的相關(guān)資料,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • Vue3封裝全局函數(shù)式組件方法總結(jié)

    Vue3封裝全局函數(shù)式組件方法總結(jié)

    函數(shù)式組件就是沒有管理任何狀態(tài),也沒有監(jiān)聽任何傳遞給它的狀態(tài),也沒有生命周期方法,它只是一個(gè)接受一些 prop 的函數(shù),下面這篇文章主要給大家介紹了關(guān)于Vue3封裝全局函數(shù)式組件方法的相關(guān)資料,需要的朋友可以參考下
    2023-06-06

最新評(píng)論