2021-12-21 09:27:52 +08:00
|
|
|
<template>
|
|
|
|
<el-upload
|
|
|
|
ref="upload"
|
|
|
|
class="upload-demo"
|
|
|
|
action="https://jsonplaceholder.typicode.com/posts/"
|
|
|
|
:limit="1"
|
|
|
|
:on-exceed="handleExceed"
|
|
|
|
:auto-upload="false"
|
|
|
|
>
|
|
|
|
<template #trigger>
|
2021-12-28 19:38:23 +08:00
|
|
|
<el-button type="primary">select file</el-button>
|
2021-12-21 09:27:52 +08:00
|
|
|
</template>
|
2022-03-23 18:50:36 +08:00
|
|
|
<el-button class="ml-3" type="success" @click="submitUpload">
|
|
|
|
upload to server
|
|
|
|
</el-button>
|
2021-12-21 09:27:52 +08:00
|
|
|
<template #tip>
|
2022-03-23 18:50:36 +08:00
|
|
|
<div class="el-upload__tip text-red">
|
2021-12-21 09:27:52 +08:00
|
|
|
limit 1 file, new file will cover the old file
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</el-upload>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { ref } from 'vue'
|
2022-03-23 18:50:36 +08:00
|
|
|
import { genFileId } from 'element-plus'
|
2022-03-25 15:35:56 +08:00
|
|
|
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
|
2021-12-21 09:27:52 +08:00
|
|
|
|
2022-03-23 18:50:36 +08:00
|
|
|
const upload = ref<UploadInstance>()
|
|
|
|
|
|
|
|
const handleExceed: UploadProps['onExceed'] = (files) => {
|
|
|
|
upload.value!.clearFiles()
|
|
|
|
const file = files[0] as UploadRawFile
|
|
|
|
file.uid = genFileId()
|
|
|
|
upload.value!.handleStart(file)
|
2021-12-21 09:27:52 +08:00
|
|
|
}
|
2022-03-23 18:50:36 +08:00
|
|
|
|
2021-12-21 09:27:52 +08:00
|
|
|
const submitUpload = () => {
|
2022-03-23 18:50:36 +08:00
|
|
|
upload.value!.submit()
|
2021-12-21 09:27:52 +08:00
|
|
|
}
|
|
|
|
</script>
|