Upload.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <a-upload :before-upload="beforeUpload" v-model:file-list="fileList" @change="handleChange">
  3. <a-button type="primary"> Upload </a-button>
  4. </a-upload>
  5. </template>
  6. <script lang="ts">
  7. import { defineComponent, reactive, toRefs } from 'vue';
  8. import { Upload } from 'ant-design-vue';
  9. import { uploadApi } from '/@/api/sys/upload';
  10. interface FileItem {
  11. uid: string;
  12. name?: string;
  13. status?: string;
  14. response?: string;
  15. url?: string;
  16. }
  17. interface FileInfo {
  18. file: FileItem;
  19. fileList: FileItem[];
  20. }
  21. interface State {
  22. fileList: FileItem[];
  23. }
  24. export default defineComponent({
  25. components: {
  26. [Upload.name]: Upload,
  27. },
  28. setup() {
  29. const state = reactive<State>({
  30. fileList: [
  31. {
  32. uid: '1',
  33. name: 'xxx.png',
  34. status: 'done',
  35. response: 'Server Error 500', // custom error message to show
  36. url: 'http://www.baidu.com/xxx.png',
  37. },
  38. {
  39. uid: '2',
  40. name: 'yyy.png',
  41. // status: 'done',
  42. url: 'http://www.baidu.com/yyy.png',
  43. },
  44. {
  45. uid: '3',
  46. name: 'zzz.png',
  47. status: 'error',
  48. response: 'Server Error 500', // custom error message to show
  49. url: 'http://www.baidu.com/zzz.png',
  50. },
  51. ],
  52. });
  53. function beforeUpload(file) {
  54. console.log(`file`, file);
  55. uploadApi({ file })
  56. .then((res) => {
  57. console.log(`res`, res);
  58. })
  59. .catch((err) => {
  60. console.log(`err`, err);
  61. });
  62. return false;
  63. }
  64. const handleChange = ({ file, fileList }: FileInfo) => {
  65. console.log(`file`, file);
  66. console.log(`fileList`, fileList);
  67. if (file.status !== 'uploading') {
  68. console.log(file, fileList);
  69. }
  70. };
  71. return {
  72. beforeUpload,
  73. handleChange,
  74. ...toRefs(state),
  75. };
  76. },
  77. });
  78. </script>