fileItem.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <div class="uploadWrapper">
  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. :on-preview="handlePreview"
  15. >
  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 {
  23. listFileBusinessId,
  24. delFile,
  25. downloadFileById,
  26. } from "@/api/system/file";
  27. export default {
  28. props: {},
  29. data() {
  30. return {
  31. fileList: [],
  32. uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
  33. headers: {
  34. Authorization: "Bearer " + getToken(),
  35. },
  36. };
  37. },
  38. mounted() {
  39. if (this.id) {
  40. this.getListFileBusinessId(this.id);
  41. }
  42. },
  43. methods: {
  44. // 根据附件业务ID()获取附件详情信息列表
  45. getListFileBusinessId(id) {
  46. if (id) {
  47. listFileBusinessId(id).then((response) => {
  48. if (response.data.length > 0) {
  49. let list = response.data;
  50. for (let i in list) {
  51. list[i].name = list[i].newUploadName;
  52. }
  53. this.fileList = list;
  54. this.$emit("getFileList", this.fileList);
  55. }
  56. });
  57. }
  58. },
  59. handleChange(file, fileList) {
  60. // console.log("----", file, fileList);
  61. },
  62. handleRemove(file, fileList) {
  63. this.fileList = fileList;
  64. if (file.id) {
  65. const id = file.id;
  66. delFile(id).then((response) => {
  67. // 给父组件传值
  68. this.$emit("getFileList", this.fileList);
  69. });
  70. }
  71. },
  72. beforeRemove(file, fileList) {
  73. return this.$confirm(`确定移除 ${file.name}?`);
  74. },
  75. handleUploadSuccess(res, file, fileList) {
  76. let that = this;
  77. if (fileList.every((item) => item.status == "success")) {
  78. fileList.map((item) => {
  79. if (item.response) {
  80. item.response.file.name = item.response.file.newUploadName;
  81. this.fileList.push(item.response.file);
  82. that.$emit("getFileList", this.fileList);
  83. }
  84. });
  85. }
  86. },
  87. //点击文件列表中已上传的文件时的钩子
  88. handlePreview(file) {
  89. downloadFileById(file.id).then((response) => {
  90. const url = window.URL.createObjectURL(new Blob([response]));
  91. const link = document.createElement("a");
  92. link.href = url;
  93. link.setAttribute("download", file.newUploadName);
  94. document.body.appendChild(link);
  95. link.click();
  96. });
  97. },
  98. },
  99. };
  100. </script>
  101. <style lang="scss">
  102. .uploadWrapper ::v-deep .el-upload-list__item {
  103. transition: none !important;
  104. }
  105. </style>