selectFile.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div>
  3. <el-upload
  4. multiple
  5. :action="`${path}/fileUploadAndDownload/upload?noSave=1`"
  6. :headers="{ 'x-token': userStore.token }"
  7. :on-error="uploadError"
  8. :on-success="uploadSuccess"
  9. :show-file-list="true"
  10. :file-list="fileList"
  11. :limit="limit"
  12. :accept="accept"
  13. class="upload-btn"
  14. >
  15. <el-button type="primary">上传文件</el-button>
  16. </el-upload>
  17. </div>
  18. </template>
  19. <script setup>
  20. import { ref, watch } from 'vue'
  21. import { ElMessage } from 'element-plus'
  22. import { useUserStore } from '@/pinia/modules/user'
  23. defineOptions({
  24. name: 'UploadCommon',
  25. })
  26. const props = defineProps({
  27. modelValue: {
  28. type: Array,
  29. default: () => []
  30. },
  31. limit: {
  32. type: Number,
  33. default: 3
  34. },
  35. accept: {
  36. type: String,
  37. default: ''
  38. },
  39. })
  40. const path = ref(import.meta.env.VITE_BASE_API)
  41. const userStore = useUserStore()
  42. const fullscreenLoading = ref(false)
  43. const fileList = ref(props.modelValue)
  44. const emits = defineEmits(['update:modelValue'])
  45. watch(fileList.value, (val) => {
  46. console.log(val)
  47. emits('update:modelValue', val)
  48. })
  49. watch(
  50. () => props.modelValue,
  51. value => {
  52. fileList.value = value
  53. },
  54. { immediate: true }
  55. )
  56. const uploadSuccess = (res) => {
  57. const { data } = res
  58. if (data.file) {
  59. fileList.value.push({
  60. name: data.file.name,
  61. url: data.file.url
  62. })
  63. fullscreenLoading.value = false
  64. }
  65. }
  66. const uploadError = () => {
  67. ElMessage({
  68. type: 'error',
  69. message: '上传失败'
  70. })
  71. fullscreenLoading.value = false
  72. }
  73. </script>