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

element-ui中el-upload多文件一次性上傳的實(shí)現(xiàn)

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

項(xiàng)目需求是多個文件上傳,在一次請求中完成,而ElementUI的上傳組件是每個文件發(fā)一次上傳請求,因此我們借助FormData的格式向后臺傳文件組

html代碼

<div class="upload-file">
 <el-upload
   accept=".xlsx"
   ref="upload"
   multiple
   :limit="5"
   action="http://xxx.xxx.xxx/personality/uploadExcel"
   :on-preview="handlePreview"
   :on-change="handleChange"
   :on-remove="handleRemove"
   :on-exceed="handleExceed"
   :file-list="fileList"
   :http-request="uploadFile"
   :auto-upload="false">
   <el-button slot="trigger" size="small" type="primary">選取文件</el-button>
   <el-button style="margin-left: 133px;" size="small" type="success" @click="submitUpload">上傳到服務(wù)器
   </el-button>
   <div slot="tip" class="el-upload__tip">只能上傳xlsx文件,且不超過100m</div>
 </el-upload>
 </div>

修改:auto-upload="false"屬性為false,阻止組件的自動上傳
:http-request="uploadFile"覆蓋上傳事件
@click=“submitUpload”,給上傳按鈕綁定事件

在這里插入圖片描述

data() {
	return {
		fileData: '', // 文件上傳數(shù)據(jù)(多文件合一)
		fileList: [],  // upload多文件數(shù)組
		uploadData: {
     fieldData: {
      id: '', // 機(jī)構(gòu)id,
     }
    },
	}
}


methods:{
		// 上傳文件
		uploadFile(file) {
		  this.fileData.append('files', file.file); // append增加數(shù)據(jù)
		},
		
		// 上傳到服務(wù)器
	   submitUpload() {
	      let fieldData = this.uploadData.fieldData; // 緩存,注意,fieldData不要與fileData看混
	      if (fieldData.id === '') {
	        this.$message({
	          message: '請選擇上傳機(jī)構(gòu)',
	          type: 'warning'
	        })
	      } else if (this.fileList.length === 0) {
	        this.$message({
	          message: '請先選擇文件',
	          type: 'warning'
	        })
	      } else {
	        const isLt100M = this.fileList.every(file => file.size / 1024 / 1024 < 100);
	        if (!isLt100M) {
	          this.$message.error('請檢查,上傳文件大小不能超過100MB!');
	        } else {
	          this.fileData = new FormData(); // new formData對象
	          this.$refs.upload.submit(); // 提交調(diào)用uploadFile函數(shù)
	          this.fileData.append('pathId', fieldData.id); // 添加機(jī)構(gòu)id
	          this.fileData.append('loginToken', this.loginToken); // 添加token
	          post(this.baseUrlData.url_02 + ":8090/personality/uploadExcel", this.fileData).then((response) => {
	            if (response.data.code === 0) {
	              this.$message({
	                message: "上傳成功",
	                type: 'success'
	              });
	              this.fileList = [];
	            } else {
	              this.$message({
	                message: response.data.desc,
	                type: 'error'
	              })
	            }
	          });
	        }
	      }
	    },
	    
      //移除
      handleRemove(file, fileList) {
        this.fileList = fileList;
        // return this.$confirm(`確定移除 ${ file.name }?`);
      },

      // 選取文件超過數(shù)量提示
      handleExceed(files, fileList) {
        this.$message.warning(`當(dāng)前限制選擇 5 個文件,本次選擇了 ${files.length} 個文件,共選擇了 ${files.length + fileList.length} 個文件`);
      },	   

			 //監(jiān)控上傳文件列表
      handleChange(file, fileList) {
        let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name);
        if (existFile) {
          this.$message.error('當(dāng)前文件已經(jīng)存在!');
          fileList.pop();
        }
        this.fileList = fileList;
      },
}

此時就達(dá)到上傳4個文件只發(fā)送了一個xhr請求了

在這里插入圖片描述

到此這篇關(guān)于element-ui中el-upload多文件一次性上傳的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)el-upload多文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解vue-amap引入高德JS API的原理

    詳解vue-amap引入高德JS API的原理

    vue-amap是對高德地圖JS API進(jìn)行封裝的、適用于vue項(xiàng)目的地圖組件庫,本文主要介紹了vue-amap引入高德JS API的原理,具有一定的參考價值,感興趣的可以了解一下
    2022-06-06
  • Vue中的slot使用插槽分發(fā)內(nèi)容的方法

    Vue中的slot使用插槽分發(fā)內(nèi)容的方法

    這篇文章主要介紹了Vue中的slot使用插槽分發(fā)內(nèi)容的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • 詳解Vue中Computed與watch的用法與區(qū)別

    詳解Vue中Computed與watch的用法與區(qū)別

    這篇文章主要介紹了Vue中computed和watch的使用與區(qū)別,文中通過示例為大家進(jìn)行了詳細(xì)講解,對Vue感興趣的同學(xué),可以學(xué)習(xí)一下
    2022-04-04
  • vue-cli+iview項(xiàng)目打包上線之后圖標(biāo)不顯示問題及解決方法

    vue-cli+iview項(xiàng)目打包上線之后圖標(biāo)不顯示問題及解決方法

    這篇文章主要介紹了解決vue-cli+iview項(xiàng)目打包上線之后圖標(biāo)不顯示問題,本文通過兩種方法給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • 最新評論