上传文件

上传文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<script setup>
import { ref, getCurrentInstance } from 'vue';

const {proxy} = getCurrentInstance()

const props = defineProps({
modelValue: {
type: Object,
default: null
},
imageUrlPrefix: {
type: String,
}
})
//如果父组件传来一个图片地址, 就用父组件的, 否则展示用户上传的图片

const localPreview = ref(false)
const localFile = ref(null)

const emit = defineEmits(['update:modelValue'])
const selectorFile = async(file) => {
file = file.file
let img = new FileReader()
img.readAsDataURL(file)
img.onload = ({target}) => {
localFile.value = target.result
}
localPreview.value = true
emit("update:modelValue", file)
}
</script>

<template>
<div class="attachment-selector">
<el-upload
name="file"
:show-file-list="false"
accept=".zip, .rar"
:multiple="false"
:http-request="selectorFile"
>
<div class="attachment-selector-btn">
<template v-if="localFile">
<img :src="localFile">
</template>
<template v-else>
<img
:src="(imageUrlPrefix ? imageUrlPrefix : proxy.globalInfo.imageUrl)
+ modelValue.imageUrl"
v-if="modelValue && modelValue.imageUrl"
>
<span class="iconfont icon-add"></span>
</template>
</div>
</el-upload>
<template
v-if="modelValue">
<div
class="file-name"
:title="modelValue.name">
{{ modelValue.name }}
</div>
<div class="iconfont iconfont-del"></div>
</template>
</div>
</template>

<style scoped>
.attachment-selector {
.attachment-selector-btn {
background: #dddddd3d;
width: 150px;
height: 150px;
display: flex;
align-items: center;
justify-content: center;
img {
max-width: 100%;
height: auto;
}
.iconfont {
font-size: 50px;
color: #ddd;
}
}
}
</style>

上传附件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<script setup>
import { getCurrentInstance } from 'vue';

const {proxy} = getCurrentInstance()

const props = defineProps({
modelValue: {
type: Object,
default: null
},
imageUrlPrefix: {
type: String,
}
})

const emit = defineEmits(['update:modelValue'])
const selectorFile = async(file) => {
file = file.file
emit("update:modelValue", file)
}

const delFile = () => {
emit("update:modelValue", null)
}
</script>

<template>
<div class="attachment-selector">
<el-upload
name="file"
:show-file-list="false"
accept=".zip, .rar"
:multiple="false"
:http-request="selectorFile"
>
<el-button
type="primary"
v-if="!modelValue">上传附件</el-button>
</el-upload>
<template
v-if="modelValue">
<div
class="file-name"
:title="modelValue.name">
{{ modelValue.name }}
</div>
<div
class="iconfont icon-del"
@click="delFile"
></div>
</template>
</div>
</template>

<style scoped>
.attachment-selector {
width: 100%;
display: flex;
.iconfont {
cursor: pointer;
font-size: 19px;
color: var(--link);
}
.file-name {
white-space: nowrap;
overflow: hidden;
color: #9b9b9b;
flex: 1;
}
}
</style>