element中的el-upload附件上傳與附件回顯
開發(fā)中經(jīng)常也會遇到附件的上傳和回顯,最方便的就是我們封裝一個公共組件在頁面中引用
1、上傳
在src里面新建一個文件夾
<template> <el-upload class="upload-demo" multiple :limit="limit" :accept="accept" :headers="headers" :file-list="fileList" :action="uploadImgUrl" :on-exceed="handleExceed" :on-remove="handleRemove" :on-success="handleSuccess" :before-upload="beforeUpload" :on-error="handleUploadError"> <!-- :on-preview="handlePreview" --> <el-button size="small" type="primary">點擊上傳</el-button> <div slot="tip" class="el-upload__tip" style="color:#909399"> 支持上傳{{ accept === "*" ? "所有" : accept }}文件,且不超過20MB,附件名稱不能超過50字 </div> </el-upload> </template>
<script> import { getToken } from '@/utils/auth' export default { components: {}, props: { value: { type: String, default: null }, accept: { type: String, default: '*' }, limit: { type: Number, default: 1 } }, data () { return { uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上傳的圖片服務(wù)器地址 headers: { Authorization: 'Bearer ' + getToken() }, fileList: [], returnData: [] } }, watch: {}, mounted () { this.value === null && this.value === '' ? (this.fileList = []) : this.getFileList() }, methods: { getFileList () { if (this.value !== null && this.value !== '') { const accessory = JSON.parse(this.value) this.fileList = [] accessory.map(val => { this.fileList.push({ name: val.name, // 編輯修改 // response: { // fileName: val.url // }, response: { data: { fileName: val.url } } }) }) } }, // 刪除上傳文件后 handleRemove (file, fileList) { this.getReturnData(fileList) }, // 上傳前------------文件大小和格式校驗 beforeUpload (file) { if (this.accept !== '*') { var fileType = file.name.substring(file.name.lastIndexOf('.') + 1) const accept = this.accept.split(',') if (accept.indexOf('.' + fileType) < 0) { this.$message.warning('請選擇正確的文件格式') return false } } const isLt50M = file.size / 1024 / 1024 < 20 if (!isLt50M) { this.$message.warning('上傳附件大小不能超過 20MB!') return false } return true }, // 附件上傳成功后的鉤子 handleSuccess (response, file, fileList) { if (response.code === 200) { this.getReturnData(fileList) } else { this.$message.error(response.msg) this.fileList=[] } }, handleUploadError () { this.$message({ type: 'error', message: '上傳失敗' }) }, // 獲取附件信息后進行返回處理 getReturnData (fileList) { this.returnData = [] console.log(fileList) fileList.map(val => { this.returnData.push({ name: val.name, url: val.response.data.fileName }) }) this.$emit('input', JSON.stringify(this.returnData)) }, // 點擊上傳文件的鉤子 handlePreview (file) { var a = document.createElement('a', '_bank') var event = new MouseEvent('click') a.download = file.name a.href = file.response.url a.dispatchEvent(event) }, // 文件超出個數(shù)限制時的鉤子 handleExceed (files, fileList) { this.$message.warning(`當(dāng)前限制只能上傳 ` + this.limit + ` 個文件`) }, // 刪除文件之前的鉤子,參數(shù)為上傳的文件和文件列表,若返回 false 或者返回 Promise 且被 reject,則停止刪除。 beforeRemove (file, fileList) { return this.$confirm(`確定移除 ${file.name}?`) } } } </script>
代碼中這個import { getToken } from ‘@/utils/auth’,其實是獲取的存在Cookies里面的token的值,大家可以根據(jù)自己項目情況進行修改使用
在頁面中直接引入使用就可以了:
2、附件回顯
和上傳一樣同樣新建一個文件夾
<template> <div class="upload-detail"> <div class="detail" style="line-height:20px" v-if="accessory === '[]' || accessory === null">無</div> <div v-else> <template v-for="(val,key) in upload.fileList"> <el-link :key="key" class="detail" :underline="false" @click="handlePreview(val)">{{val.name}} <span class="icon-emad- icon-emad-xiazai"> 下載</span> </el-link> </template> </div> </div> </template>
<script> export default { data () { return { upload: { // 上傳的地址 url: process.env.VUE_APP_BASE_API + "/common/upload", // 上傳的文件列表 fileList: [] } } }, props: { accessory: { type: String, default: '[]' } }, created () { this.getInfo(this.accessory) }, methods: { getInfo (accessory) { this.upload.fileList = [] if (accessory !== '[]' || accessory !== null) { let list = JSON.parse(accessory) list.map(val => { this.upload.fileList.push({ name: val.name, url:process.env.VUE_APP_BASE_API + val.url }) }) } }, // 點擊上傳----------------文件下載 handlePreview (file) { var a = document.createElement('a'); var event = new MouseEvent('click'); a.download = file.name; a.href = file.url; a.dispatchEvent(event); } } }; </script>
<style lang="scss" scoped> ::v-deep .el-upload-list { height: auto; overflow: hidden; } .detail { line-height: 20px; display: block; .icon-emad- { color: rgba(98, 173, 255, 1); } } </style>
頁面中引入使用:
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用cesium創(chuàng)建數(shù)據(jù)白模方式
這篇文章主要介紹了vue使用cesium創(chuàng)建數(shù)據(jù)白模方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue+echarts繪制省份地圖并添加自定義標(biāo)注方式
這篇文章主要介紹了vue+echarts繪制省份地圖并添加自定義標(biāo)注方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04Vue3路由配置createRouter、createWebHistory、useRouter和useRoute詳解
Vue3和Vue2基本差不多,只不過需要將createRouter、createWebHistory從vue-router中引入,再進行使用,下面這篇文章主要給大家介紹了關(guān)于Vue3路由配置createRouter、createWebHistory、useRouter和useRoute的相關(guān)資料,需要的朋友可以參考下2023-02-02