解讀element-ui使用el-upload,before-upload函數(shù)不好使的問題
element-ui使用el-upload,before-upload函數(shù)不好使
在使用el-upload這個組件的時候,業(yè)務(wù)是需要傳其他參數(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 | 是否在選取文件后立即進行上傳 | boolean | — | true |
這里有個地方需要注意:
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ā),而設(shè)置了 :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ù)導(dǎo)入 ? ? ? ? ?? ?<span style="color:red">*</span>: ? ? ? ? </span> ? ? ? ? <el-button ? ? ? ? ?? ?slot="trigger" ? ? ? ? ?? ?size="small" ? ? ? ? ?? ?type="primary" ? ? ? ? ?? ?style="float: right;" ? ? ? ? > ? ? ? ? ?? ?瀏覽 ? ? ? ? </el-button> ? ? ?</el-upload>
?// 反饋數(shù)據(jù)導(dǎo)出 ? ? 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)文章
vue 倒計時結(jié)束跳轉(zhuǎn)頁面實現(xiàn)代碼
在商場大家經(jīng)??吹酵\囀召M倒計時支付頁面,今天小編通過本文給大家分享vue 倒計時結(jié)束跳轉(zhuǎn)頁面功能,感興趣的朋友一起看看吧2023-10-10Vue.js學(xué)習(xí)筆記之 helloworld
vue是法語中視圖的意思,Vue.js是一個輕巧、高性能、可組件化的MVVM庫,同時擁有非常容易上手的API。有需要的小伙伴可以參考下2016-08-08ElementUI Tag組件實現(xiàn)多標簽生成的方法示例
這篇文章主要介紹了ElementUI Tag組件實現(xiàn)多標簽生成的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07