最近有個需求,須要在上傳文件前,能夠進行彈窗控制是否上傳upload
看完文檔後,感受有兩種思路能夠實現json
before-upload
:上傳文件以前的鉤子,參數爲上傳的文件,若返回 false 或者返回 Promise 且被 reject,則中止上傳。auto-upload, on-change
手動上傳來控制before-upload
初始代碼服務器
// template <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :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> // javscript data() { return { imageUrl: "" }; }, methods: { handleAvatarSuccess(res, file) { this.imageUrl = URL.createObjectURL(file.raw); }, beforeAvatarUpload(file) { // 文件類型進行判斷 const isJPG = file.type === "image/jpeg"; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error("上傳頭像圖片只能是 JPG 格式!"); } if (!isLt2M) { this.$message.error("上傳頭像圖片大小不能超過 2MB!"); } return isJPG && isLt2M; } }
初始效果圖
異步
考慮在before-upload中進行彈窗後,
return false | reject()
便可
修改代碼async
因爲
this.$confirm
是異步操做,於是須要等待其結果才能進行下一步操做函數
async beforeAvatarUpload(file) { const isSubmit = await this.$confirm('此操做將上傳文件, 是否繼續?', '提示', { confirmButtonText: '肯定', cancelButtonText: '取消', type: 'warning' }).then(() => { return true }).catch(() => { return false }); console.log(isSubmit) return isSubmit; }
確認提交和取消提交 ==> 結果卻同樣post
確認提交
jsonp
取消提交
this
結果卻不能夠,於是考慮另外一種思路了,
before-upload
只是進行判斷文件是否適合,從而是否上否上傳到服務器,而不是用來等待用戶進行操做使用的code
auto-upload
設置爲文件導入後是否立刻上傳到服務器on-change
用來查看文件如今的轉態// template <el-upload ref="upload" class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :limit="1" :auto-upload="false" :on-change="handleChange" :show-file-list="true" :file-list="fileList" :on-error="handleError" :on-success="handleSuccess"> <el-button size="small" type="primary">點擊上傳</el-button> </el-upload> // js data() { return { fileList: [ ], bool: true } }, methods: { handleRemove(file, fileList) { console.log(file, fileList); }, handlePreview(file) { console.log(file); }, handleError(err, file) { alert('失敗') this.fileList = [] }, handleSuccess(res, file) { alert('成功') this.fileList = [] }, handleExceed(files, fileList) {}, async handleChange() { const isSubmit = await this.$confirm('此操做將永久刪除該文件, 是否繼續?', '提示', { confirmButtonText: '肯定', cancelButtonText: '取消', type: 'warning' }).then(() => { return false }).catch(() => { return true }); if (isSubmit) { this.$refs.upload.submit() } else { this.fileList = [] } } }
此方法可行,如今就是控制由於文件狀態改變而致使兩次彈窗, 修改以下component
添加flag標識進行控制彈窗便可
data() { return { isConfirm: true } } async handleChange() { if (!this.isConfirm) { this.isConfirm = true return } const bo = await this.$confirm('此操做將永久刪除該文件, 是否繼續?', '提示', { confirmButtonText: '肯定', cancelButtonText: '取消', type: 'warning' }).then(() => { return false }).catch(() => { return true }) if (bo) { this.$refs.upload.submit() this.isConfirm = false } else { this.fileList = [] } }
修改後即可以了,只是注意 在
on-error
和on-succes
中注意清空fileList = []
,這樣還能夠從新添加文件
截止目前任務算是完成,只是依舊有些扎心,只好繼續篤定前行了 沒事的時候,仍是多看文檔,這樣才能避免不要的坑,以及能夠有更多的思路