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

解讀element-ui使用el-upload,before-upload函數(shù)不好使的問題

 更新時間:2023年03月28日 09:46:25   作者:拿回憶下酒  
這篇文章主要介紹了解讀element-ui使用el-upload,before-upload函數(shù)不好使的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

element-ui使用el-upload,before-upload函數(shù)不好使

在使用el-upload這個組件的時候,業(yè)務是需要傳其他參數(shù)給后臺,所以校驗寫在before-upload中,在before-upload中直接返回true或者是false,依然會發(fā)文件給后臺

參數(shù)說明類型可選值默認值
on-progress文件上傳時的鉤子function(event, file, fileList) — —
on-change文件狀態(tài)改變時的鉤子,添加文件、上傳成功和上傳失敗時都會被調(diào)用function(file, fileList)
before-upload上傳文件之前的鉤子,參數(shù)為上傳的文件,若返回 false 或者返回 Promise 且被 reject,則停止上傳。function(file)
auto-upload是否在選取文件后立即進行上傳booleantrue

這里有個地方需要注意:

before-upload 是上傳前的校驗,因此auto-upload必須為true

解決方式

我這里是采用在函數(shù)中返回一個promise來解決的:

<template>
	<el-upload
	  class="avatar-uploader"
	  action="https://xxx.xxx.com/xxx/xxx"
	  :show-file-list="false"
	  :on-success="handleAvatarSuccess"
	  :before-upload="beforeAvatarUpload">
	  <img v-if="imageUrl" :src="imageUrl" class="avatar">
	  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
	</el-upload>
</template>
<script>
export default {
	data() {
      return {
        imageUrl: ''
      };
    },
	methods: {
		beforeAvatarUpload (file) {
	      return new Promise(async (resolve, reject) => {
	      	// 失敗
	        if ('xxx' !=0) {
	          reject()
	        } else {
	        	// 成功
	        	resolve()
			}
	      })
	    },
	    handleAvatarSuccess(res, file) {
	        this.imageUrl = URL.createObjectURL(file.raw);
	    }
	 }
 }
 </script>

ElementUI el-upload上傳圖片限制,before-upload不生效

因為 before-upload 是指在文件上傳之前、文件已被選中,但還沒上傳的時候觸發(fā),而設置了 :auto-upload=“false” 后,文件上傳事件不被再次調(diào)用,所以 before-upload 不生效,所以,限制圖片大小和格式的時候,需綁定在 :on-change 里面

??? ?<el-upload
??? ??? ?class="upload-demo uploadTwo"
??? ??? ?ref="fileUploadRef"
??? ??? ?:action="fileUrl + 'order/mdm/partpredictioncoord/import'"
??? ??? ?:file-list="fileUploadList"
??? ??? ?:auto-upload="false"
??? ??? ?:headers="header"
??? ??? ?name="uploadFile"
??? ??? ?:limit="1" multiple
??? ??? ?:on-change="beforeFeedBackExport"
??? ??? ?:on-success="fileUploadSuccess">
? ? ? ? <span style="float: left; line-height: 32px; padding-right: 10px">反饋數(shù)據(jù)導入
? ? ? ? ?? ?<span style="color:red">*</span>:
? ? ? ? </span>
? ? ? ? <el-button
? ? ? ? ?? ?slot="trigger"
? ? ? ? ?? ?size="small"
? ? ? ? ?? ?type="primary"
? ? ? ? ?? ?style="float: right;"
? ? ? ? >
? ? ? ? ?? ?瀏覽
? ? ? ? </el-button>
? ? ?</el-upload>
?// 反饋數(shù)據(jù)導出
? ? beforeFeedBackExport(file) {

? ? ? // this.tableFileName = file.name;


? ? ? let testFile = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()

? ? ? const extension = testFile === 'xlsx' || testFile === 'xls';

? ? ? const isLt2M = (file.size / 1024 / 1024 < 10);
? ? ? if (!extension) {
? ? ? ? this.$message({
? ? ? ? ? message: '上傳文件只能是xls/xlsx!',
? ? ? ? ? type: 'warning'
? ? ? ? });
? ? ? ? this.fileUploadList = []
? ? ? ? return false;
? ? ? }
? ? ? if (!isLt2M) {
? ? ? ? this.$message({
? ? ? ? ? message: "文件大小不可以超過10M",
? ? ? ? ? type: 'warning'
? ? ? ? });
? ? ? ? this.fileUploadList = []
? ? ? ? return false;
? ? ? }
? ? ? return (extension) && isLt2M
? ? },

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

寿宁县| 安塞县| 金湖县| 鹤庆县| 威信县| 阿坝县| 开封市| 玉门市| 隆安县| 来安县| 寿阳县| 阳泉市| 大渡口区| 台北县| 甘洛县| 正宁县| 谢通门县| 大关县| 石阡县| 呼图壁县| 吴川市| 遂宁市| 信丰县| 兴和县| 柘城县| 镇雄县| 龙里县| 五峰| 天气| 阳高县| 辉南县| 会宁县| 新丰县| 兴海县| 易门县| 西城区| 北海市| 大港区| 米林县| 奎屯市| 永安市|