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

el-upload實(shí)現(xiàn)上傳文件并展示進(jìn)度條功能

 更新時(shí)間:2023年05月24日 14:40:37   作者:滿城風(fēng)絮,梅子黃時(shí)雨  
這篇文章主要介紹了el-upload實(shí)現(xiàn)上傳文件并展示進(jìn)度條,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

el-upload實(shí)現(xiàn)上傳文件并展示進(jìn)度條

el-upload在實(shí)現(xiàn)文件上傳時(shí)無(wú)法觸發(fā)on-progress鉤子,即使調(diào)用后端接口并成功的情況下都無(wú)法觸發(fā),可以通過(guò)如下配置來(lái)解決:

const config = {
        onUploadProgress: progressEvent => {
          if (progressEvent.lengthComputable) {
            this.uploadProgress = Math.round((progressEvent.loaded / progressEvent.total) * 100)
            console.log('progressEvent>>', progressEvent)
            console.log('uploadProgress>>', _this.uploadProgress)
          }
        }
      }

隨后將config添加至調(diào)用后端接口,即可成功獲取進(jìn)度~

html:

前端-上傳文件獲取進(jìn)度條:
  <el-upload
            v-show="!fileList.length"
            ref="fileUpload"
            class="upload-demo"
            style="display: inline-block;height: 32px;margin-left: 8px"
            action="#"
            :file-list="fileList"
            :http-request="uploadVersion"
            :on-change="handleChange"
            :on-success="handleSuccess"
            :on-progress="handleProgress"
            :on-error="handleError"
            :auto-upload="true"
            :show-file-list="false"
          >
            <!--            icon_upload.svg-->
            <el-button type="primary" style="height: 32px;display: flex;align-items: center"><svg-icon icon-class="icon_upload" style="margin-right: 8px" />上傳文件</el-button>
            <!--            <el-input v-model="uploadForm.filename" placeholder="請(qǐng)選擇">-->
            <!--              &lt;!&ndash;              <template slot="append"><el-button&ndash;&gt;-->
            <!--              &lt;!&ndash;                size="mini"&ndash;&gt;-->
            <!--              &lt;!&ndash;              >&ndash;&gt;-->
            <!--              &lt;!&ndash;                上傳文件&ndash;&gt;-->
            <!--              &lt;!&ndash;              </el-button></template>&ndash;&gt;-->
            <!--            </el-input>-->
          </el-upload>
          <!--          <el-button size="small" @click="sendClick">上傳</el-button>-->
          <div v-if="fileElProgress">
            <div class="el-progress-div">
              <div><div
                v-loading="true"
                style="display: inline-block;width: 24px;
             height: 16px;
             padding-right: 8px;"
              />{{ fileName }}</div>
              <span>
                <span style="display: inline-block;margin-right: 8px">{{ progressPercent }}%</span>
                <el-button type="text" @click="cancelUpload">取消</el-button>
              </span>
            </div>
            <el-progress :percentage="progressPercent" :text-inside="false" :color="customColors" :stroke-width="2" />
            <!--            :stroke-width="16" status="scuccess"-->
          </div>
          <template v-if="!fileElProgress">
            <div v-for="item in fileList" :key="item.name" class="fail-list">
              <div class="list-item">
                <span :style="{color:showFailTip?'#FF3D00':'#fff' }">
                  <svg-icon :icon-class="showFailTip? 'icon_error_file': 'icon_success_file'" />
                  {{ item.name }}
                </span>
                <span style="float: right;display: flex;align-items: center;">
                  <span style="color: #878D99;display: inline-block;margin-right: 16px">{{ showFailTip ? '失敗':'成功' }}</span>
                  <!--                  <span>{{ '失敗' }}</span>-->
                  <el-button style="color: #00E3FF" type="text" size="small" @click="fileList = []">刪除</el-button>
                  <el-button v-show="showFailTip" style="color: #00E3FF" type="text" size="small" @click="sendClick">重新上傳</el-button>
                </span>
              </div>
            </div>
          </template>

JS部分

 data() {
    return {
      // 進(jìn)度條
      fileList: [],
      showFailTip: false,
      customColors: [
        { color: 'rgba(223,228,237,0.10)', percentage: 0 },
        { color: '#00adc9', percentage: 100 }
      ],
      fileElProgress: false,
      fileProgressText: '',
      progressPercent: 0,
     }
  methodss:{
   uploadVersion(params) {
      const _this = this
      this.uploadForm.filename = params.file.name
      this.fileFormData = new FormData()
      this.fileFormData.append('file', params.file)
      this.fileFormData.append('md5File', params.file)
      this.fileName = params.file.name
      const config = {
        onUploadProgress: progressEvent => {
          if (progressEvent.lengthComputable) {
            _this.uploadProgress = Math.round((progressEvent.loaded / progressEvent.total) * 100)
            console.log('progressEvent>>', progressEvent)
            console.log('uploadProgress>>', _this.uploadProgress)
            this.fileElProgress = true
            if (this.progressPercent < 99) {
              this.progressPercent = _this.uploadProgress
            } else {
              this.fileProgressText = '正在上傳'
            }
          }
        }
      }
      uploadFile(this.fileFormData, config).then(res => {
        if (res.data === 'success') {
          this.fileProgressText = '上傳成功'
        } else {
          this.showFailTip = true
        }
        this.fileElProgress = false
      })
    },
  }
  },

el-upload的使用方法

基本使用方法

el-upload的基本使用方法很簡(jiǎn)單,參考官網(wǎng)的例子即可,這里記錄幾個(gè)常用的屬性

<el-col :span="12">
   <el-form-item label="附件" prop="attachments">
     <el-upload
       class="upload"
       name="file"
       :action="doUpload"
       :headers="headers"
       :before-remove="beforeRemove"
       :limit="3"
       :on-exceed="handleExceed"
       :before-upload="handleBeforeUpload"
       :on-success="uploadSuccess"
       :multiple="true"
       :file-list="fileList"
     >
       <el-button type="text" size="small" icon="el-icon-upload">點(diǎn)擊上傳</el-button>
     </el-upload>
   </el-form-item>
 </el-col>
  • class:可以自己修改一下樣式
  • name:這個(gè)name很重要,錯(cuò)了后臺(tái)接收不到文件,官方是這樣解釋的 上傳的文件字段名 ,實(shí)際上就是后端對(duì)應(yīng)的接口參數(shù)的名字,后端可能是這么定義的 public AjaxResult uploadFile(MultipartFile file) throws Exception
  • action:就是后端接收文件的接口地址
  • headers:有些程序用token作為鑒權(quán)依據(jù),通常把token放在header中,headers長(zhǎng)這樣子:headers: { Authorization: this.$store.getters.token }
  • beforeRemove:刪除之前可以彈出個(gè)提示框,問(wèn)問(wèn)要不要?jiǎng)h除,就像這樣 : return this.$confirm(確定移除 ${file.name}?)
  • on-exceed:官方這么解釋文件超出個(gè)數(shù)限制時(shí)的鉤子 ,一般應(yīng)用這個(gè)鉤子彈出一個(gè)提示信息,告訴用戶文件個(gè)數(shù)超過(guò)限制了
  • before-upload:官方這么解釋上傳文件之前的鉤子,參數(shù)為上傳的文件,若返回 false 或者返回 Promise 且被 reject,則停止上傳。

我們可以在上傳之前判斷一下用戶選擇的文件是不是符合要求,比如文件類(lèi)型是否正確、文件大小是否超限等。例如:

handleBeforeUpload(file) {
	  const uploadLimit = 2
	  const uploadTypes = ['jpg', 'png', 'doc', 'docx', 'xlsx', 'zip', 'rar', 'pdf']
      const filetype = file.name.replace(/.+\./, '')
      const isRightSize = (file.size || 0) / 1024 / 1024 < uploadLimit
      if (!isRightSize) {
        this.$message.error('文件大小超過(guò) ' + uploadLimit + 'MB')
        return false
      }
      if (uploadTypes.indexOf(filetype.toLowerCase()) === -1) {
        this.$message.warning({
          message: '請(qǐng)上傳后綴名為jpg、png、txt、pdf、doc、docx、xlsx、zip或rar的附件'
        })
        return false
      }
      return true
    }
  • multiple:是否支持選擇多個(gè)文件
  • file-list:在查看數(shù)據(jù)的時(shí)候,如果我們要回顯已經(jīng)上傳的文件,就需要設(shè)置這個(gè)屬性了
fileList = [{name:"附件4.png",
fileName:"/profile/upload/2020/07/21/7e6735cbc848c40a2f2242b56d09fe58.png"},
{name:"新建文本文檔.txt",
fileName:"/profile/upload/2020/07/21/34c6389353856c9429405360eaec864d.txt"}]

到此這篇關(guān)于el-upload實(shí)現(xiàn)上傳文件并展示進(jìn)度條的文章就介紹到這了,更多相關(guān)el-upload上傳文件并展示進(jìn)度條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解原生js實(shí)現(xiàn)offset方法

    詳解原生js實(shí)現(xiàn)offset方法

    本篇文章主要介紹了原生js實(shí)現(xiàn)offset方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • BootstrapVue選項(xiàng)卡標(biāo)題增加關(guān)閉按鈕的方法

    BootstrapVue選項(xiàng)卡標(biāo)題增加關(guān)閉按鈕的方法

    這篇文章主要為大家詳細(xì)介紹了BootstrapVue選項(xiàng)卡標(biāo)題增加關(guān)閉按鈕的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • JavaScript實(shí)現(xiàn)前端飛機(jī)大戰(zhàn)小游戲

    JavaScript實(shí)現(xiàn)前端飛機(jī)大戰(zhàn)小游戲

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)前端飛機(jī)大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • JavaScript中的函數(shù)聲明和函數(shù)表達(dá)式區(qū)別淺析

    JavaScript中的函數(shù)聲明和函數(shù)表達(dá)式區(qū)別淺析

    這篇文章主要介紹了JavaScript中的函數(shù)聲明和函數(shù)表達(dá)式區(qū)別淺析,本文總結(jié)的淺顯易懂,非常好的一篇技術(shù)文章,需要的朋友可以參考下
    2015-03-03
  • uniapp圖片展示自適應(yīng)等比例縮放方法舉例

    uniapp圖片展示自適應(yīng)等比例縮放方法舉例

    這篇文章主要給大家介紹了關(guān)于uniapp圖片展示自適應(yīng)等比例縮放方法的相關(guān)資料,在uniapp頁(yè)面展示中會(huì)遇到圖片展示問(wèn)題,等比縮放或者自適應(yīng)view大小,需要的朋友可以參考下
    2023-10-10
  • iframe實(shí)用操作錦集

    iframe實(shí)用操作錦集

    這篇文章主要介紹了有關(guān)iframe的實(shí)用操作,包括高度、傳遞數(shù)據(jù)等等,感興趣的朋友可以參考下
    2014-04-04
  • JavaScript工具庫(kù)jscpd檢測(cè)前端代碼重復(fù)度

    JavaScript工具庫(kù)jscpd檢測(cè)前端代碼重復(fù)度

    在前端開(kāi)發(fā)中,代碼的重復(fù)度是一個(gè)常見(jiàn)的問(wèn)題,重復(fù)的代碼不僅增加了代碼的維護(hù)成本,還可能導(dǎo)致程序的低效運(yùn)行,為了解決這個(gè)問(wèn)題,有許多工具和技術(shù)被用來(lái)檢測(cè)和消除代碼重復(fù),其中一個(gè)被廣泛使用的工具就是jscpd
    2023-10-10
  • JavaScript中var與let的區(qū)別

    JavaScript中var與let的區(qū)別

    這篇文章主要介紹了JavaScript中var與let的區(qū)別,var是JavaScript剛出現(xiàn)時(shí)就存在的變量聲明關(guān)鍵字,而let作為ES6才出現(xiàn)的變量聲明關(guān)鍵字,無(wú)疑兩者之間存在著很大的區(qū)別,下面來(lái)看看兩者之間到底存在什么
    2021-12-12
  • ES6學(xué)習(xí)教程之對(duì)象的擴(kuò)展詳解

    ES6學(xué)習(xí)教程之對(duì)象的擴(kuò)展詳解

    這篇文章主要給大家介紹了ES6中對(duì)象擴(kuò)展的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-05-05
  • Webpack devServer中的 proxy 實(shí)現(xiàn)跨域的解決

    Webpack devServer中的 proxy 實(shí)現(xiàn)跨域的解決

    這篇文章主要介紹了Webpack devServer中的 proxy 實(shí)現(xiàn)跨域的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06

最新評(píng)論

丁青县| 寻乌县| 双辽市| 梁河县| 普格县| 澜沧| 永丰县| 五峰| 卓资县| 天长市| 宝应县| 于都县| 高青县| 潜江市| 高陵县| 屏东市| 苏尼特右旗| 乌海市| 乐陵市| 紫金县| 大冶市| 镇平县| 宁乡县| 黎川县| 靖安县| 阿拉善右旗| 涟源市| 宁海县| 阿克陶县| 东港市| 桓仁| 吴川市| 五常市| 麻栗坡县| 临颍县| 历史| 天全县| 舟山市| 手游| 榆中县| 吉隆县|