FileUpload.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <a-modal
  3. v-model:open="open"
  4. title="上传文档"
  5. :footer="null"
  6. width="600px"
  7. :maskClosable="false"
  8. >
  9. <a-upload-dragger
  10. style="margin: 20px 0 10px; width: 100%"
  11. v-model:fileList="fileList"
  12. name="file"
  13. :multiple="true"
  14. :maxCount="1"
  15. accept=".pdf"
  16. :beforeUpload="handleUpload"
  17. >
  18. <p class="ant-upload-drag-icon">
  19. <inbox-outlined></inbox-outlined>
  20. </p>
  21. <p class="ant-upload-text">点击上传或拖入文档</p>
  22. <p class="ant-upload-hint">文档单个最大50MB;当前仅支持PDF格式</p>
  23. </a-upload-dragger>
  24. </a-modal>
  25. </template>
  26. <script lang="ts" setup>
  27. /**
  28. * @description 文件上传组件
  29. */
  30. import { ref, defineExpose } from "vue";
  31. import { InboxOutlined } from "@ant-design/icons-vue";
  32. import { message } from "ant-design-vue";
  33. import http from "@/utils/http";
  34. const props = defineProps<{
  35. pid?: number
  36. }>();
  37. const emits = defineEmits<{
  38. (e: 'close'):void
  39. }>()
  40. // 弹窗部分
  41. const open = ref<boolean>(false);
  42. const showModal = () => {
  43. open.value = true;
  44. };
  45. // 文件上传部分
  46. const fileList = ref<any[]>([]);
  47. const handleUpload = async (file: any) => {
  48. if(props.id === null) return;
  49. if(!file) return;
  50. const sizeNum = parseFloat((file.size / 1024 / 1024).toFixed(2))
  51. if(sizeNum > 50){
  52. message.error("上传文件不能大于50MB!");
  53. return;
  54. }
  55. const urlStr = '/ai/knowledge/file/create'
  56. const formData = new FormData();
  57. formData.append("parentId", props.pid)
  58. formData.append("file", file); // 文件对象
  59. formData.append("name", file.name); // 用户id
  60. formData.append("size", file.size);
  61. formData.append("type", file.type);
  62. http.post(urlStr, formData).then((result) => {
  63. if (result.data) {
  64. message.success("文件导入成功!");
  65. const timer = setTimeout(()=>{
  66. open.value = false;
  67. emits('close')
  68. clearTimeout(timer)
  69. }, 1500)
  70. } else {
  71. message.error("文件导入失败,请稍后重试!");
  72. }
  73. });
  74. return false;
  75. };
  76. defineExpose({ showModal });
  77. </script>