element plus中el-upload實(shí)現(xiàn)上傳多張圖片的示例代碼
element中圖片上傳的一些要點(diǎn)
1、想要實(shí)現(xiàn)多圖片上傳一定要標(biāo)注multiple屬性、而且一定要是自己手動(dòng)實(shí)現(xiàn)上傳功能、這里有兩個(gè)方法:
(1)通過(guò)配置file-list,即獲取file-list中上傳的文件然后封裝到FormData()中,最后調(diào)用axios傳遞formData數(shù)據(jù)到后臺(tái)
(2)重寫http-request="uploadFile"方法,重寫了這個(gè)方法后調(diào)用’this.$refs.upload.submit(),就會(huì)自動(dòng)調(diào)用你重寫的http-request=""方法,它會(huì)根據(jù)你上傳的圖片個(gè)數(shù)循環(huán)調(diào)用多少次。如你上傳了兩張,調(diào)用’this.$refs.upload.submit()后內(nèi)部會(huì)自動(dòng)調(diào)用兩次http-request=""方法。這樣我們?cè)趆ttp-request=""方法中設(shè)置this.fileList.append(‘file’, item.file)就能將文件封裝進(jìn)一個(gè)list中,然后再封裝進(jìn)formData。最后調(diào)用axios即可。
注意: 上面兩種方法,使用方式是一樣的,都是自己封裝好一個(gè)FormData,然后調(diào)用axios,而且axios傳遞數(shù)據(jù)一定是data:yourData樣式的,千萬(wàn)不要自討苦吃這樣寫query:youData
2、element圖片組件是默認(rèn)上傳的,添加屬性auto-upload="false"后才能關(guān)閉。也只有關(guān)閉了自動(dòng)提交后、才能實(shí)現(xiàn)手動(dòng)提交
3、element圖片組件自動(dòng)上傳如果沒(méi)有手動(dòng)封裝FormData數(shù)據(jù)然后調(diào)用axios,即使添加了multiple屬性可以上傳多個(gè),element圖片組件只會(huì)是一個(gè)圖片發(fā)送一個(gè)請(qǐng)求的,而并非是一個(gè)請(qǐng)求多個(gè)圖片
4、調(diào)用this.$refs.upload.submit(),element圖片組件會(huì)使用默認(rèn)的上傳方式上傳文件 如果重寫了http-reques方法,那么久會(huì)調(diào)用http-reques方法,默認(rèn)上傳和’this.$refs.upload.submit()'請(qǐng)求url都是element圖片組件上綁定的action,只有自己調(diào)用axios才不會(huì)使用到這個(gè)action
5、如果想要一個(gè)請(qǐng)求上傳多個(gè)圖片并附帶參數(shù)、只能使用formData.append("你的屬性名",屬性值)的方式,使用formData.append("實(shí)體類名",this.form)是無(wú)法成功傳數(shù)據(jù)的
6、關(guān)閉自動(dòng)上傳后調(diào)用this.$refs.upload.submit()才能生效,默認(rèn)上傳請(qǐng)求url都會(huì)是element圖片組件上綁定的action,自動(dòng)上傳都是自己調(diào)用axios上傳的
實(shí)現(xiàn)自定義下載、刪除、查看

<template>
<el-dialog :close-on-click-modal="false" :model-value="dialogVisibleBox" :before-close="handleCloseUploadImg">
<el-upload
ref="uploadRef"
v-model:file-list="fileList"
list-type="picture-card"
:on-change="onChangeFun"
:auto-upload="false"
action="#">
<el-icon>
<Plus/>
</el-icon>
<template #file="{ file }">
<div>
<img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
<span class="el-upload-list__item-actions">
<span
class="el-upload-list__item-preview"
@click="handlePictureCardPreview(file)"
>
<el-icon><zoom-in /></el-icon>
</span>
<span
v-if="!disabled"
class="el-upload-list__item-delete"
@click="handleDownload(file)"
>
<el-icon><Download /></el-icon>
</span>
<span
v-if="!disabled"
class="el-upload-list__item-delete"
@click="handleRemoveFun(file)"
>
<el-icon><Delete /></el-icon>
</span>
</span>
</div>
</template>
</el-upload>
<template #footer>
<span class="dialog-footer">
<el-button @click="handleCloseUploadImg">取消</el-button>
<el-button type="primary" @click="saveData">
確認(rèn)
</el-button>
</span>
</template>
<el-dialog v-model="dialogVisible">
<img w-full :src="dialogImageUrl" alt="Preview Image"/>
</el-dialog>
</el-dialog>
</template>
<script setup lang="ts">
import { ref, toRefs, watch } from 'vue';
import {ElNotification, UploadProps, UploadUserFile} from 'element-plus';
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue';
const props = defineProps({
dialogVisibleBox: Boolean
});
let {dialogVisibleBox} = toRefs(props);
const uploadRef: any = ref();
let fileList: any = reactive<UploadUserFile[]>([
// {
// : 'food.jpeg',
// url: '../../../assets/image/order/blue_light_2.png',
// }
]);
const saveImgList: any = {};
const dialogImageUrl = ref('');
let disabled = ref(false);
const dialogVisible = ref(false);
let saveUploadImgNum: number = 0;
const onChangeFun = (rawFile: any) => {
if (rawFile.status !== "ready") return;
const maxSize = 30;
const imgSize = rawFile.size / 1024 / 1024 < maxSize;
const imgType = ['image/png', 'image/jpg', 'image/jpeg'].includes(rawFile.raw.type);
if (!imgType)
ElNotification({
title: '溫馨提示',
message: '請(qǐng)上傳png、jpg、jpeg文件格式的圖片!',
type: 'warning',
});
if (!imgSize)
ElNotification({
title: '溫馨提示',
message: `上傳圖片大小不能超過(guò) ${maxSize}M!`,
type: 'warning',
});
if (imgType && imgSize) {
saveUploadImgNum++;
}
};
// 傳遞關(guān)閉事件
const emit = defineEmits(['handleCloseUploadImg']);
const handleCloseUploadImg = () => {
// 清空upload列表
uploadRef.value.clearFiles();
emit("handleCloseUploadImg");
};
const handleRemoveFun= async (file: any) => {
// 這里可以先處理后端刪除 前端再刪除
const index = fileList.indexOf(file);
if (index !== -1) { // 確保文件存在于文件列表中
saveUploadImgNum--;
fileList.splice(index, 1); // 從文件列表中刪除文件
}
}
const handlePictureCardPreview = (file: any) => {
// 方法查看
dialogImageUrl.value = file.url!;
dialogVisible.value = true;
}
const handleDownload = (file: any) => {
// 下載
const link: any = document.createElement('a');
link.download = file.name;
link.href = file.url;
link.click();
window.URL.revokeObjectURL(link.href);
}
</script>到此這篇關(guān)于element plus中el-upload實(shí)現(xiàn)上傳多張圖片的示例代碼的文章就介紹到這了,更多相關(guān)element el-upload上傳多張圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- element-ui中el-upload多文件一次性上傳的實(shí)現(xiàn)
- Element el-upload上傳組件使用詳解
- elementui使用el-upload組件如何實(shí)現(xiàn)自定義上傳
- 如何在ElementUI的上傳組件el-upload中設(shè)置header
- 解決ElementUI組件中el-upload上傳圖片不顯示問(wèn)題
- 解讀element?el-upload上傳的附件名稱不顯示?file-list賦值
- element?el-upload文件上傳覆蓋第一個(gè)文件的實(shí)現(xiàn)
- elementui?el-upload一次請(qǐng)求上傳多個(gè)文件的實(shí)現(xiàn)
- elementui使用el-upload組件實(shí)現(xiàn)自定義上傳的詳細(xì)步驟
相關(guān)文章
Vue 使用依賴注入的方式共享數(shù)據(jù)的過(guò)程
賴注入的方式共享數(shù)據(jù)在Vue中是一種高級(jí)特性,它主要用于開(kāi)發(fā)插件或庫(kù),或者處理一些特殊的場(chǎng)景,這篇文章主要介紹了Vue 使用依賴注入的方式共享數(shù)據(jù),需要的朋友可以參考下2023-11-11
Vue?項(xiàng)目的成功發(fā)布和部署的實(shí)現(xiàn)
本文主要介紹了Vue?項(xiàng)目的成功發(fā)布和部署的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Vue中transition單個(gè)節(jié)點(diǎn)過(guò)渡與transition-group列表過(guò)渡全過(guò)程
這篇文章主要介紹了Vue中transition單個(gè)節(jié)點(diǎn)過(guò)渡與transition-group列表過(guò)渡全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue實(shí)現(xiàn)某元素吸頂或固定位置顯示(監(jiān)聽(tīng)滾動(dòng)事件)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)某元素吸頂或固定位置顯示,監(jiān)聽(tīng)滾動(dòng)事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
vue3中使用router4 keepalive的問(wèn)題
這篇文章主要介紹了vue3中使用router4 keepalive的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

