關于Element上傳組件beforeUpload上傳前限制失效問題
Element上傳組件beforeUpload上傳前限制失效
在before-upload(上傳文件之前的鉤子 參數為上傳的文件 若返回 false 或者返回 Promise 且被 reject 則停止上傳)鉤子里去做判斷 這里有一個坑
當你設置了 :auto-upload="false" 的時候, 這個鉤子是不會被觸發(fā)的,因此也可以在on-change中做判斷。
beforeUpload(file, fileList) { if (file.size / (1024 * 1024) > 500) { // 限制文件大小 this.$message.warning(`當前限制文件大小不能大于500M`) return false } let suffix = this.getFileType(file.name) //獲取文件后綴名 let suffixArray = ['jpg', 'png', 'jpeg', 'gif'] //限制的文件類型,根據情況自己定義 if (suffixArray.indexOf(suffix) === -1) { this.$message({ message: '文件格式錯誤', type: 'error', duration: 2000 }) this.$refs.uploadPhoto.handleRemove(file); } return suffixArray }, getFileType(name) { let startIndex = name.lastIndexOf('.') if (startIndex !== -1) { return name.slice(startIndex + 1).toLowerCase() } else { return '' } }
element中before-upload不起作用的問題 、on-change上傳文件大小限制
1、因為設置了:auto-upload="false"
:on-change="changeImgClick"方法 和 before-upload 發(fā)生了沖突 導致before-upload 方法不起作用
如果有:auto-upload="false" 屬性就要用:on-change 方法監(jiān)聽
2、on-change上傳文件大小限制
當文件超過20MB的時候讓他提示文件大小不能超過20MB,請重新上傳。
下面我們來看代碼:
<el-upload ? ?class="upload-demo" ref="upload" name="upload" :action="action()" ? ? :on-change="(file, fileList) => {handleChange(file, fileList, scope);} " ? ? :on-remove=" (file, fileList) => {handleRemove(file, scope);}" ? ? :file-list="scope.row.fileList" ? ? :auto-upload="false" ? ? ?> ? ? ? ?<el-button ?slot="trigger" size="mini" type="primary" v-if="scope.row.fileList.length == 0" >上傳文件</el-button> </el-upload>
首先在el-upload的:on-change事件里的handleChange的方法中可以獲取上傳文件的大小
handleChange(file, fileList, scope) { ?? ??? ?//獲取上傳文件大小 ? ? ? let imgSize = Number(file.size / 1024 / 1024); ? ? ? ? if (imgSize > 20) {? ? ? ? ? this.$msgbox({ ? ? ? ? ? title: "", ? ? ? ? ? message: "文件大小不能超過20MB,請重新上傳。", ? ? ? ? ? type: "warning", ? ? ? ? }); ? ? ? ? this.materialList[scope.$index].fileList.splice(scope.index, 1); ? ? ? ? return; ? ? ? }? ? ? ? ? ?//后面可以不用 我自己也沒有用到 ? ? ? this.text = "上傳中"; ? ? ? ?? ? ? ? this.loading = true; ? ? ? this.materialList[scope.$index].fileList.push(file); ? ? ? let data = new FormData(); ? ? ? data.append("files", file.raw); ? ? ? uploadFile(data, scope.row.materialName).then((response) => { ? ? ? ? if (response.success) { ? ? ? ? ? this.loading = false; ? ? ? ? ? this.listedFiles[scope.$index] = response.result[0].id; ? ? ? ? } else { ? ? ? ? ? this.loading = false; ? ? ? ? ? this.msgError(response.message || "操作失敗"); ? ? ? ? } ? ? ? }); ? ? },
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 使用element upload上傳組件如何去掉刪除按鈕
- element?el-upload文件上傳覆蓋第一個文件的實現
- 解決elementui上傳組件el-upload無法第二次上傳問題
- elementUI使用el-upload上傳文件寫法及避坑總結(上傳圖片/視頻到本地/服務器及回顯+刪除)
- element-ui中el-upload多文件一次性上傳的實現
- element-ui 中使用upload多文件上傳只請求一次接口
- vue-cli3.0+element-ui上傳組件el-upload的使用
- element-ui upload組件多文件上傳的示例代碼
- element UI upload組件上傳附件格式限制方法
- 在vue項目中使用element-ui的Upload上傳組件的示例
- Element中Upload組件上傳功能實現(圖片和文件的默認上傳及自定義上傳)
相關文章
Vue中使用elementui與Sortable.js實現列表拖動排序
這篇文章主要為大家詳細介紹了Vue中使用elementui與Sortable.js實現列表拖動排序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04