element-plus中el-upload組件限制上傳文件類型的方法
element-plus中el-upload組件限制上傳文件類型
Element Plus 中,el-upload 組件可以通過設(shè)置 accept 屬性來限制上傳文件的格式
1.限制上傳圖片文件
<el-upload accept="image/*" action="/upload" :on-success="handleSuccess" > <el-button slot="trigger" size="small" type="primary">點(diǎn)擊上傳</el-button> </el-upload>
2.限制上傳 Excel 文件
<el-upload accept=".xls,.xlsx" action="/upload" :on-success="handleSuccess" > <el-button slot="trigger" size="small" type="primary">點(diǎn)擊上傳</el-button> </el-upload>
Element-plus upload上傳限制文件類型,文件大小
html部分:
<template> <el-upload v-model:file-list="fileList" class="upload-demo" :http-request="uploadFile" multiple :on-preview="handlePreview" :on-remove="handleRemove" :on-success="message" :before-remove="beforeRemove" :limit="1" :on-exceed="handleExceed" > <el-button type="primary">選擇文件</el-button> <template #tip> <div class="el-upload__tip"> 支持格式:zip,ppt,pdf,word,excel,csv,png,jpg,單個文件不能超過20MB </div> </template> </el-upload> </template>
JS部分:
// 上傳文件 const uploadFile = async (file) => { const allowedFileTypes = [ "application/vnd.ms-excel", // Microsoft Excel 表格 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", //xlx "application/zip", // Zip files "application/vnd.ms-powerpoint", // PowerPoint files "application/pdf", // PDF files "application/msword", // Word files "application/vnd.openxmlformats-officedocument.wordprocessingml.document", //docx "text/csv", // CSV files "image/png", // PNG images "image/jpeg", // JPG images ]; const maxSize = 20 * 1024 * 1024; // 20MB if (file) { const options = { meta: { temp: "demo" }, mime: "json", headers: { "Content-Type": "text/plain" }, }; const isLt20M = file.file.size <= maxSize; if (!isLt20M) { ElMessage({ type: "error", message: "文件大小超過20MB", }); fileList.value = []; return false; // 阻止文件上傳 } if (!allowedFileTypes.includes(file.file.type)) { ElMessage({ type: "error", message: "文件格式錯誤", }); fileList.value = []; return false; // 阻止文件上傳 } try { //向后端上傳文件 const result = await client.value.put(file.file.name, file.file, options); oosurl.value = result.url; } catch (e) { // Handle the error } } else { ElMessage.warning({ message: "No file selected", type: "warning", }); }
到此這篇關(guān)于element-plus中el-upload組件限制上傳文件類型的文章就介紹到這了,更多相關(guān)element-plus限制上傳文件類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對Vue2 自定義全局指令Vue.directive和指令的生命周期介紹
今天小編就為大家分享一篇對Vue2 自定義全局指令Vue.directive和指令的生命周期介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08vue3+electron12+dll開發(fā)客戶端配置詳解
本文將結(jié)合實(shí)例代碼,介紹vue3+electron12+dll客戶端配置,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06Vue實(shí)現(xiàn)簡單可擴(kuò)展甘特圖的方法詳解
Ganttastic是一個小型的Vue.js組件,用于在Web應(yīng)用程序上呈現(xiàn)一個可配置的、可拖動的甘特圖。本文就將用它來實(shí)現(xiàn)簡單可擴(kuò)展的甘特圖,感興趣的可以嘗試一下2022-11-11vue使用exif獲取圖片旋轉(zhuǎn),壓縮的示例代碼
這篇文章主要介紹了vue使用exif獲取圖片旋轉(zhuǎn),壓縮的示例代碼,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-12-12Vue自定義組件雙向綁定實(shí)現(xiàn)原理及方法詳解
這篇文章主要介紹了Vue自定義組件雙向綁定實(shí)現(xiàn)原理及方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09Vue.js實(shí)現(xiàn)點(diǎn)擊左右按鈕圖片切換
這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)點(diǎn)擊左右按鈕圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07vue中點(diǎn)擊下載圖片的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue中點(diǎn)擊下載圖片的實(shí)現(xiàn)方法,在Vue的模板中,我們可以將下載屬性綁定至或元素上,用來實(shí)現(xiàn)點(diǎn)擊下載,需要的朋友可以參考下2023-08-08