最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

element-ui多文件上傳的實(shí)現(xiàn)示例

 更新時(shí)間:2019年04月10日 10:58:19   作者:crystal  
這篇文章主要介紹了element-ui多文件上傳的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

上傳方案一:

先將文件上傳到七牛,再將七牛上傳返回的文件訪問路徑上傳到服務(wù)器

<div class="upload-music-container">
  <el-upload
   class="upload-music"
   ref="upload"
   action="http://up-z2.qiniup.com/"
   :data="{token:uploadToken}"
   multiple
   accept=".mp3"
   :before-upload="uploadBefore"
   :on-change="uploadChange"
   :on-success="uploadSuccess"
   :on-error="uploadError">
   <el-button size="small" type="primary">選取文件</el-button>
   <div slot="tip" class="el-upload__tip">僅支持上傳mp3文件,文件大小不超過500M</div>
  </el-upload>
  <el-button size="small" type="success" @click="submitUpload">上傳到服務(wù)器</el-button>
</div>
 

export default {
  name: 'uploadMusic',
  data() {
   return {
    headers: {},
    uploadToken: null,
    canUploadMore: true,
    fileList: null,
   }
  },
  created() {
   this.headers = {}   //此處需要與server約定具體的header
   this.getUploadToken()
  },
  methods: {
   //獲取上傳七牛token憑證
   getUploadToken() {
    this.$http.get('xxxxxxx', {headers: this.headers}).then(response => {
     if (response.data.status == 200) {
      let resp = response.data.data
      this.uploadToken = resp.token
     } else {
      this.$message({
       message: '獲取憑證失敗,請重試',
       type: 'error'
      })
     }
    })
   },
   //獲取音頻文件時(shí)長
   getVideoPlayTime(file, fileList) {
    let self = this
    //獲取錄音時(shí)長
    try {
     let url = URL.createObjectURL(file.raw);
     //經(jīng)測試,發(fā)現(xiàn)audio也可獲取視頻的時(shí)長
     let audioElement = new Audio(url);
     let duration;
     audioElement.addEventListener("loadedmetadata", function (_event) {
      duration = audioElement.duration;
      file.duration = duration
      self.fileList = fileList
     });
    } catch (e) {
     console.log(e)
    }
   },
   //校驗(yàn)上傳文件大小
   uploadChange(file, fileList) {
    this.fileList = fileList
    let totalSize = 0
    for (let file of fileList) {
     totalSize += file.raw.size
    }
    if (totalSize > 500 * 1024 * 1024) {
     this.canUploadMore = false
     this.$message({
      message: '上傳文件不能不超過500M',
      type: 'warn'
     })
    } else {
     this.canUploadMore = true
    }
   },
   uploadBefore(file) {
    if (this.canUploadMore) {
     return true
    }
    return false
   },
   //上傳成功
   uploadSuccess(response, file, fileList) {
    this.getVideoPlayTime(file, fileList)
   },
   //上傳失敗
   uploadError(err, file, fileList) {
    console.log(err)
   },
   //上傳服務(wù)器數(shù)據(jù)格式化
   getUploadMusicList() {
    let musicList = []
    for (let file of this.fileList) {
     if (file.response && file.response.key) {
      musicList.push({
       "play_time": file.duration, //播放時(shí)長
       "size": file.size/1024,   //文件大小 單位 kb
       "song_name": file.name,   //歌曲名
       "voice_url": "xxxx"     //上傳七牛返回的訪問路徑
      })
     }
    }
    return musicList
   },
   //上傳至服務(wù)器
   submitUpload() {
    let musicList = this.getUploadMusicList()
    this.$http.post('xxxxxxxxxx', {music_list: musicList}, {headers: this.headers}).then(response => {
     if (response.data.status == 200) {
      this.$refs.upload.clearFiles() //上傳成功后清空文件列表
      this.$message({
       message: '上傳服務(wù)器成功',
       type: 'success'
      })
     } else{
      this.$message({
       message: '上傳服務(wù)器失敗,請重試',
       type: 'error'
      })
     }
    }).catch(err => {
     this.$message({
      message: '上傳服務(wù)器失敗,請重試',
      type: 'error'
     })
    })
   },
  }
 }

上傳方案二:

直接將文件上傳到服務(wù)器

<div class="upload-music-container">
  <el-upload
   class="upload-music"
   ref="upload"
   multiple
   action=""
   :auto-upload="false"
   :http-request="uploadFile">
   <el-button slot="trigger" size="small" type="primary">選取文件</el-button>
   <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上傳到服務(wù)器</el-button>
   <div slot="tip" class="el-upload__tip">只能上傳mp3文件,且單次不超過500M</div>
  </el-upload>
</div>

export default {
  name: 'uploadMusic',
  data() {
   return {
    fileType:'video',
    fileData: new FormData(),
    headers:{},
   }
  },

補(bǔ)充:element-ui實(shí)現(xiàn)多文件加表單參數(shù)上傳

element-ui是分圖片多次上傳,一次上傳一個(gè)圖片。

如果想一次上傳多個(gè)圖片,就得關(guān)掉自動上傳:auto-upload=‘false',同時(shí)不使用element內(nèi)置上傳函數(shù),換成自己寫的onsubmit()

為了實(shí)現(xiàn)圖片的添加刪除,可在on-change與on-remove事件中取得filelist(filelist實(shí)質(zhì)就是uploadFiles的別名,而uploadFiles就是element內(nèi)置的用于保存待上傳文件或圖片的數(shù)組),在最后一步提交的過程中,將filelist中的值一一添加到formdata對象中(formdata.append()添加,formdata.delete()刪除),然后統(tǒng)一上傳。

ps:on-preview事件和<el-dialog>組件以及對應(yīng)屬性、方法這一體系是用來實(shí)現(xiàn)圖片的點(diǎn)擊放大功能。被注釋掉的beforeupload只有一個(gè)實(shí)參,是針對單一文件上傳時(shí)使用到的,這里無法用上

<template>
 <div>
  <el-upload
   action="http://127.0.0.1:8000/api/UploadFile/"
   list-type="picture-card"
   :auto-upload="false"
   :on-change="OnChange"
   :on-remove="OnRemove"
   :on-preview="handlePictureCardPreview"
   :before-remove="beforeRemove"
   >
   <i class="el-icon-plus"></i>
  </el-upload>
  <el-dialog :visible.sync="dialogVisible">
   <img width="100%" :src="dialogImageUrl" alt="">
  </el-dialog>
  <el-button type="" @click="fun">點(diǎn)擊查看filelist</el-button>
  <el-button type="" @click="onSubmit">提交</el-button>
 </div>
</template>
 
<script>
import {host,batchTagInfo} from '../../api/api'
export default {
  data() {
   return {
    param: new FormData(),
    form:{},
    count:0,
    fileList:[],
    dialogVisible:false,
    dialogImageUrl:''
   };
  },
  methods: {
   handlePictureCardPreview(file) {
    this.dialogImageUrl = file.url;
    this.dialogVisible = true;
   },
   beforeRemove(file, fileList) {
    return this.$confirm(`確定移除 ${ file.name }?`);
   },
   OnChange(file,fileList){
    this.fileList=fileList
 
   },
   OnRemove(file,fileList){
    this.fileList=fileList
   },
   //阻止upload的自己上傳,進(jìn)行再操作
   // beforeupload(file) {
   //   console.log('-------------------------')
   //   console.log(file);
   //   //創(chuàng)建臨時(shí)的路徑來展示圖片
   //   //重新寫一個(gè)表單上傳的方法
   //   this.param = new FormData();
   //   this.param.append('file[]', file, file.name);
   //   this.form={
   //    a:1,
   //    b:2,
   //    c:3
   //   }
   //   // this.param.append('file[]', file, file.name);
   //   this.param.append('form',form)
   //   return true;
   // },
   fun(){
    console.log('------------------------')
    console.log(this.fileList)
   },
   onSubmit(){
     this.form={
      a:1,
      b:2,
      c:3
     }
     let file=''
    for(let x in this.form){
 
     this.param.append(x,this.form[x])
    }
    for(let i=0;i<this.fileList.length;i++){
     file='file'+this.count
     this.count++
     this.param.append(file,this.fileList[i].raw)
    }
    batchTagInfo(this.param) 
     .then(res=>{
      alert(res)
     })
   }
  }
 }
</script>
<style> 
</style>

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

相關(guān)文章

  • 動畫詳解Vue3的Composition?Api

    動畫詳解Vue3的Composition?Api

    為讓大家更好的理解Vue3的Composition?Api本文采用了詳細(xì)的動畫演繹,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Vue3實(shí)現(xiàn)圖片放大鏡效果

    Vue3實(shí)現(xiàn)圖片放大鏡效果

    這篇文章主要為大家詳細(xì)介紹了Vue3實(shí)現(xiàn)圖片放大鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 詳解使用VueJS開發(fā)項(xiàng)目中的兼容問題

    詳解使用VueJS開發(fā)項(xiàng)目中的兼容問題

    這篇文章主要介紹了詳解使用VueJS開發(fā)項(xiàng)目中的兼容問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • VUE實(shí)現(xiàn)表單元素雙向綁定(總結(jié))

    VUE實(shí)現(xiàn)表單元素雙向綁定(總結(jié))

    本篇文章主要介紹了VUE實(shí)現(xiàn)表單元素雙向綁定(總結(jié)) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Vue 修改網(wǎng)站圖標(biāo)的方法

    Vue 修改網(wǎng)站圖標(biāo)的方法

    這篇文章主要介紹了Vue 修改網(wǎng)站圖標(biāo)的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-12-12
  • Vue.js實(shí)現(xiàn)一個(gè)SPA登錄頁面的過程【推薦】

    Vue.js實(shí)現(xiàn)一個(gè)SPA登錄頁面的過程【推薦】

    本篇文章主要介紹了Vue.js寫一個(gè)SPA登錄頁面過程的相關(guān)知識,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化的教程

    Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化的教程

    所謂數(shù)據(jù)可視化,我們可以理解為從宏觀角度來看一眼就能看出來整個(gè)數(shù)據(jù)的占比,走向,對于數(shù)據(jù)可視化,很多互聯(lián)網(wǎng)公司是很看重這一塊的,包括大廠,本就將給大家介紹如何通過Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化
    2023-06-06
  • Vue綁定內(nèi)聯(lián)樣式問題

    Vue綁定內(nèi)聯(lián)樣式問題

    這篇文章主要介紹了Vue綁定內(nèi)聯(lián)樣式的相關(guān)知識,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-10-10
  • elementUi 中table表尾插入行的實(shí)例

    elementUi 中table表尾插入行的實(shí)例

    這篇文章主要介紹了elementUi 中table表尾插入行的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 詳解Vue CLI3配置解析之css.extract

    詳解Vue CLI3配置解析之css.extract

    這篇文章主要介紹了詳解Vue CLI3配置解析之css.extract,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09

最新評論

蒙阴县| 渭南市| 法库县| 西峡县| 金平| 林州市| 吴桥县| 肇源县| 辰溪县| 开封县| 平南县| 商洛市| 贞丰县| 黄石市| 常熟市| 长子县| 莎车县| 夏河县| 吉林市| 昌宁县| 巢湖市| 汽车| 顺义区| 梁平县| 新民市| 余庆县| 涪陵区| 定襄县| 韶关市| 河西区| 嵊泗县| 西畴县| 五台县| 汨罗市| 嵊州市| 重庆市| 信宜市| 玛多县| 安泽县| 寿宁县| 广东省|