上传组件优化

This commit is contained in:
13218645326 2023-12-05 20:12:35 +08:00
parent 1e60aeb65a
commit 60a6934b28
2 changed files with 57 additions and 36 deletions

View File

@ -1,40 +1,48 @@
<template>
<div>
<div class="upload_ss_c">
<!--action="/api/abk/web/v1/resource/file" -->
<el-upload :action="actionUrl" :auto-upload="autoUpload" style="width: 100%" :on-success="(response, file) => successUpload(response, file)"
:on-error="errorUpload" :accept="acceptTypeList.join(',')" :before-upload="beforeUpload" :multiple="multiple"
:limit="maxLimit" :on-exceed="handleExceed" :file-list="fileList" :disabled="disabledFlag"
<el-upload :action="actionUrl" :auto-upload="autoUpload" style="width: 100%"
:on-success="(response, file) => successUpload(response, file)" :on-error="errorUpload"
:accept="acceptTypeList.join(',')" :before-upload="beforeUpload" :multiple="multiple" :limit="maxLimit"
:on-exceed="handleExceed" :file-list="fileList" :disabled="disabledFlag"
:on-remove="(file, fileList) => removeFile(file, fileList)" :on-preview="(file) => preview(file)"
:on-progress="(event, file, fileList) => onProgressFn(event, file, fileList)"
list-type="picture-card"
>
:on-progress="(event, file, fileList) => onProgressFn(event, file, fileList)" list-type="picture-card">
<!-- 上传的按钮 或者 icon 通过具名插槽的方式 -->
<slot name="uploadBtn"></slot>
<slot name="default"></slot>
</el-upload>
<el-progress v-if="showProcessFlag && processFlag" :percentage="loadProcess"></el-progress>
</div>
</template>
<script lang="ts" setup>
import { ref ,nextTick} from "vue"
import { ref, nextTick } from "vue"
import { ElMessage } from 'element-plus'
import {Base64} from 'js-base64'
import { Base64 } from 'js-base64'
const props = defineProps({
actionUrl: {
//
type: String,
default: '',
},
autoUpload:{//
type:Boolean,
default:false
width: {
//
type: String,
default: '72px',
},
height: {
//
type: String,
default: '72px',
},
autoUpload: {//
type: Boolean,
default: false
},
acceptTypeList: {
//
type: Array,
default: () => {
return ['.jpg','.jpeg','.png']
return ['.jpg', '.jpeg', '.png']
// ['doc', 'docx', 'xlsx', 'xls', 'txt', 'pdf','jpg','jpeg','png','zip,'rar']
},
},
@ -61,10 +69,10 @@ const props = defineProps({
fileList: {//
type: Array,
default: () => {
return [ ]
return []
},
},
listType:{
listType: {
type: String,//'text' | 'picture' | 'picture-card'
default: 'picture',
},
@ -114,11 +122,11 @@ const officeType = ['doc', 'docx', 'xlsx', 'xls']
let processFlag = ref(false)//
let loadProcess = ref(0)//
//
const successUpload = (response:any, file:any) => {
console.log("successUpload",response,file)
const successUpload = (response: any, file: any) => {
console.log("successUpload", response, file)
if (response.rt.status === 200) {
props.fileList.push({
url: response.data,
@ -148,7 +156,7 @@ const beforeUpload = (file) => {
return false
}
if (!props.acceptTypeList.includes(name.split('.').pop())+'.') {
if (!props.acceptTypeList.includes(name.split('.').pop()) + '.') {
ElMessage({
type: 'warning',
message: `文件格式仅支持${props.acceptTypeList.join(',')}M`
@ -214,20 +222,30 @@ const preview = (data) => {
}
const onProgressFn=(event, file, fileList)=>{
processFlag.value = true
loadProcess.value = event.percent.toFixed(2)
if (loadProcess.value >= 100) {
loadProcess.value = 100
nextTick(() => {
processFlag.value = false
})
}
const onProgressFn = (event, file, fileList) => {
processFlag.value = true
loadProcess.value = event.percent.toFixed(2)
if (loadProcess.value >= 100) {
loadProcess.value = 100
nextTick(() => {
processFlag.value = false
})
}
}
</script>
<style lang="scss" scoped></style>
<style lang="scss" scoped>
:deep(.el-upload){
width: v-bind('props.width')!important;
height: v-bind('props.height')!important;
}
:deep(.el-upload-list__item){
width: v-bind('props.width')!important;
height: v-bind('props.height')!important;
}
</style>
<style></style>

View File

@ -5,11 +5,14 @@
<el-input v-model.trim="form.title" placeholder="请输入标题" clearable maxlength="30" />
</el-form-item>
<el-form-item label="图片:" prop="picture">
<uploadCom :maxLimit="3" listType="picture-card" :acceptTypeList="['.jpg','.jpeg','.png']">
<template v-slot:uploadBtn>
<el-icon size="48" color="#aaa"><Plus /></el-icon>
</template>
</uploadCom>
<uploadCom :maxLimit="3" listType="picture-card" :acceptTypeList="['.jpg','.jpeg','.png']" width="72px" height="72px">
<template v-slot:default>
<el-icon size="48" color="#aaa"><Plus /></el-icon>
</template>
</uploadCom>
</el-form-item>
<el-form-item label="跳转链接:" prop="linkUrl">
<el-input v-model.trim="form.linkUrl" clearable />