vue el-upload手動(dòng)上傳實(shí)現(xiàn)過程
vue el-upload手動(dòng)上傳
```vue
<el-upload v-model="xlsFile" ref="fileUpload" :limit="1" accept=".xls, .xlsx" action="#" :multiple="false"
:auto-upload="false" :before-upload="beforeFileUpload" :on-preview="preview" :on-change="fileChange"
:http-request="uploadHttpRequest" :on-exceed="exceed" :on-remove="fileRemove" :on-success="fileSuccess">
<el-button size="small" type="primary">
導(dǎo)入文件
<i class="el-icon-upload el-icon--right"></i>
</el-button>
<div class="el-upload__tip" style="color: rgba(255, 255, 255, 0.65)" slot="tip">
提示:僅允許上傳多1個(gè)".xlsx"或者".xls"格式文件,單個(gè)文件最大10M
</div>
</el-upload>
<el-button type="primary" @click="submitUpload()">上傳</el-button>
script部分
```vue
// 試題導(dǎo)入
beforeFileUpload(file) {
const isFileSizeLimit = file.size / 1024 / 1024 < 10;
if (!isFileSizeLimit) {
this.$message.error("單個(gè)圖片大小不能超過 10MB!");
}
return isFileSizeLimit;
},
// 文件添加、 上傳、 失敗
fileChange(file, fileList) {
this.xlsFile = file;
console.log("1111111", this.xlsFile);
},
// 文件上傳成功處理
fileSuccess(response, file, fileList) {
// this.openUpload = false;
// this.isUploading = false;
},
// 預(yù)覽文件
preview(file) {
// this.dialogVisible = true;
},
// 文件超出限制
exceed(files, fileList) {
this.$message.warning(
`當(dāng)前限制選擇 1個(gè)文件,本次選擇了 ${files.length} 個(gè)文件,共選擇了 ${
files.length + fileList.length
} 個(gè)文件`
);
},
// 移除文件
fileRemove(file, fileList) {
this.xlsFile = null;
},
// 點(diǎn)擊上傳
submitUpload() {
this.$refs.fileUpload.submit();
},
uploadHttpRequest(param) {
const formData = new FormData(); //FormData對(duì)象,添加參數(shù)只能通過append('key', value)的形式添加
formData.append("file", this.xlsFile.raw); //添加文件對(duì)象
// this.$refs["form"].validate((valid) => {
// if (valid) {
// if (!this.form.id) {
// formData.append("passValue", this.form.passValue);
// formData.append("scoreEvery", this.form.scoreEvery);
// formData.append("testName", this.form.testName);
// } else {
// formData.append("id", this.form.id);
// }
excelData(formData)
.then((res) => {
if (res.code === 200) {
this.msgSuccess("導(dǎo)入成功");
console.log('re====>',res.data)
// this.open = false;
this.getList();
}
})
.catch((err) => {
console.log("失敗", err);
param.onError(); //上傳失敗的文件會(huì)從文件列表中刪除
});
// }
// });
},
把el-upload里的自動(dòng)上傳auto-upload置為false,然后自定義上傳按鈕,調(diào)用
this.$refs.fileUpload.submit();
便會(huì)觸發(fā) :
http-request=“uploadHttpRequest”
vue在表單中使用el-upload手動(dòng)上傳圖片
自動(dòng)上傳和手動(dòng)上傳
上傳圖片分兩種,自動(dòng)上傳和手動(dòng)上傳,效果區(qū)別:

- 自動(dòng)上傳:選擇圖片后立刻調(diào)接口上傳圖片
- 手動(dòng)上傳:選擇圖片后只顯示圖片,自定義何時(shí)上傳,可以定義點(diǎn)擊確定事件里表單驗(yàn)證成功后上傳圖片
- 區(qū)別:自動(dòng)上傳會(huì)造成很多臟數(shù)據(jù),手動(dòng)上傳等到上傳時(shí)才校驗(yàn)圖片是否合規(guī)
手動(dòng)上傳
表單中使用el-upload手動(dòng)上傳圖片,組件選擇的是照片墻
<template>
<el-form
ref="cardFormRef"
:model="cardForm"
:rules="rules"
label-width="120px"
class="demo-cardForm"
status-icon
>
<el-form-item label="輪播圖" prop="photo">
<el-upload
ref="uploadRef"
:class="{ iconVis: fileList.length }"
:action="url" //上傳接口
v-model:file-list="fileList"
:limit="1" //限制上傳一張
list-type="picture-card" //照片墻
:before-upload="beforeUpload" //上傳前
:on-success="handleAvatarSuccess" //上傳成功
:headers="headers"
:auto-upload="false" //手動(dòng)上傳
>
<el-icon class="avatar-uploader-icon"><Plus /></el-icon>
</el-upload>
</el-form-item>
</el-form>
</template>var fileList = ref([]);
var uploadUrl = ref(false); //存圖片成功返回的url
const headers = ref({ Authorization: "Bearer " + getToken() });
var url =import.meta.env.VITE_APP_BASE_API + "接口";
var rules = computed(() => ({ //表單校驗(yàn)規(guī)則
photo: [
{
required: true,
message: "請(qǐng)上傳圖片",
trigger: "blur",
},
]
}));
var beforeUpload = (file) => {
console.log("上傳前");
const isJPG =
file.type === "image/jpeg" ||
file.type === "image/png" ||
file.type === "image/jpg";
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
proxy.$modal.msgError("上傳圖片只能是 JPG/PNG 格式!");
}
if (!isLt2M) {
proxy.$modal.msgError("上傳圖片大小不能超過 2MB!");
}
isJPG && isLt2M ? (uploadUrl.value = true) : (uploadUrl.value = false);
return isJPG && isLt2M;
};
function handleAvatarSuccess(res, file) {
console.log("成功了!");
let { url } = res.data;
uploadUrl.value = url;
sumbitForm(); //表單提交接口,傳uploadUrl
}
var cardFormRef=ref(null);
var uploadRef=ref(null);
var sumbit = () => { //點(diǎn)擊確定按鈕,進(jìn)行表單校驗(yàn),校驗(yàn)成功上傳圖片
cardFormRef.value.validate((val) => {
if (val) {
console.log("上傳圖片");
uploadRef.value.submit();
}
});
};點(diǎn)擊確定sumbit,表單校驗(yàn)成功 => beforeUpload檢查圖片符合規(guī)格 => handleAvatarSuccess圖片上傳成功 =>sumbitForm提交表單,包含圖片上傳成功返回的url
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+flask實(shí)現(xiàn)視頻合成功能(拖拽上傳)
這篇文章主要介紹了vue+flask實(shí)現(xiàn)視頻合成功能(拖拽上傳),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Vue報(bào)錯(cuò):Uncaught TypeError: Cannot assign to read only propert
這篇文章主要給大家介紹了關(guān)于Vue報(bào)錯(cuò):Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' 的解決方法,文中介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。2017-06-06
基于Vue2實(shí)現(xiàn)歌曲播放和歌詞滾動(dòng)效果
這篇文章主要介紹了如何基于Vue2實(shí)現(xiàn)歌曲播放和歌詞滾動(dòng)效果,文中通過代碼示例和圖文講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以自己動(dòng)手試一下2024-09-09
關(guān)于vue中hash和history的區(qū)別與使用圖文詳解
vue-router中有hash模式和history模式,下面這篇文章主要給大家介紹了關(guān)于vue中hash和history的區(qū)別與使用的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
vue項(xiàng)目中如何將數(shù)據(jù)導(dǎo)出為word文檔
這篇文章主要介紹了vue項(xiàng)目中如何將數(shù)據(jù)導(dǎo)出為word文檔問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03

