fileItem.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if (this.id) {
  37. this.getListFileBusinessId(this.id);
  38. }
  39. },
  40. methods: {
  41. // 根据附件业务ID()获取附件详情信息列表
  42. getListFileBusinessId(id) {
  43. if (id) {
  44. listFileBusinessId(id).then((response) => {
  45. if (response.data.length > 0) {
  46. let list = response.data;
  47. for (let i in list) {
  48. list[i].name = list[i].newUploadName;
  49. }
  50. this.fileList = list;
  51. }
  52. });
  53. }
  54. },
  55. handleChange(file, fileList) {
  56. // console.log("----", file, fileList);
  57. },
  58. handleRemove(file, fileList) {
  59. this.fileList = fileList;
  60. if (file.id) {
  61. const id = file.id;
  62. delFile(id).then((response) => {
  63. // 给父组件传值
  64. this.$emit("getFileList", this.fileList);
  65. });
  66. }
  67. },
  68. beforeRemove(file, fileList) {
  69. return this.$confirm(`确定移除 ${file.name}?`);
  70. },
  71. handleUploadSuccess(res, file, fileList) {
  72. let that = this;
  73. if (fileList.every((item) => item.status == "success")) {
  74. fileList.map((item) => {
  75. if (item.response) {
  76. item.response.file.name = item.response.file.newUploadName;
  77. this.fileList.push(item.response.file);
  78. that.$emit("getFileList", this.fileList);
  79. }
  80. });
  81. }
  82. },
  83. },
  84. };
  85. </script>