fileItem.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div>
  3. <el-upload
  4. ref="upload"
  5. class="upload"
  6. multiple
  7. :file-list="fileList"
  8. :action="uploadFileUrl"
  9. :headers="headers"
  10. :on-change="handleChange"
  11. :on-remove="handleRemove"
  12. :before-remove="beforeRemove"
  13. :on-success="handleUploadSuccess"
  14. >
  15. <!-- :auto-upload="false" -->
  16. <el-button size="small" type="primary">点击上传</el-button>
  17. </el-upload>
  18. </div>
  19. </template>
  20. <script>
  21. import { getToken } from "@/utils/auth";
  22. import { listFileBusinessId, delFile } from "@/api/system/file";
  23. export default {
  24. props: {
  25. },
  26. data() {
  27. return {
  28. fileList: [],
  29. uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
  30. headers: {
  31. Authorization: "Bearer " + getToken(),
  32. },
  33. };
  34. },
  35. mounted() {
  36. console.log("======文件组件")
  37. if (this.id) {
  38. this.getListFileBusinessId(this.id);
  39. }
  40. },
  41. methods: {
  42. // 根据附件业务ID()获取附件详情信息列表
  43. getListFileBusinessId(id) {
  44. if (id) {
  45. listFileBusinessId(id).then((response) => {
  46. if (response.data.length > 0) {
  47. let list = response.data;
  48. for (let i in list) {
  49. list[i].name = list[i].newUploadName;
  50. }
  51. this.fileList = list;
  52. }
  53. });
  54. }
  55. },
  56. handleChange(file, fileList) {
  57. // console.log("----", file, fileList);
  58. },
  59. handleRemove(file, fileList) {
  60. this.fileList = fileList;
  61. if (file.id) {
  62. const id = file.id;
  63. delFile(id).then((response) => {
  64. // 给父组件传值
  65. this.$emit("getFileList", this.fileList);
  66. });
  67. }
  68. },
  69. beforeRemove(file, fileList) {
  70. return this.$confirm(`确定移除 ${file.name}?`);
  71. },
  72. handleUploadSuccess(res, file, fileList) {
  73. let that = this;
  74. if (fileList.every((item) => item.status == "success")) {
  75. fileList.map((item) => {
  76. if (item.response) {
  77. item.response.file.name = item.response.file.newUploadName;
  78. this.fileList.push(item.response.file);
  79. that.$emit("getFileList", this.fileList);
  80. }
  81. });
  82. }
  83. },
  84. },
  85. };
  86. </script>