limit-cover.vue 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <template>
  2. <el-upload
  3. ref="upload"
  4. class="upload-demo"
  5. action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
  6. :limit="1"
  7. :on-exceed="handleExceed"
  8. :auto-upload="false"
  9. >
  10. <template #trigger>
  11. <el-button type="primary">select file</el-button>
  12. </template>
  13. <el-button class="ml-3" type="success" @click="submitUpload">
  14. upload to server
  15. </el-button>
  16. <template #tip>
  17. <div class="el-upload__tip text-red">
  18. limit 1 file, new file will cover the old file
  19. </div>
  20. </template>
  21. </el-upload>
  22. </template>
  23. <script setup lang="ts">
  24. import { ref } from 'vue'
  25. import { genFileId } from 'element-plus'
  26. import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
  27. const upload = ref<UploadInstance>()
  28. const handleExceed: UploadProps['onExceed'] = (files) => {
  29. upload.value!.clearFiles()
  30. const file = files[0] as UploadRawFile
  31. file.uid = genFileId()
  32. upload.value!.handleStart(file)
  33. }
  34. const submitUpload = () => {
  35. upload.value!.submit()
  36. }
  37. </script>