UploadButton.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <el-upload
  3. :action="uploadUrl"
  4. :headers="headers"
  5. :max-size="maxSize"
  6. :format="format"
  7. :accept="accept"
  8. :data="uploadDataDict"
  9. :before-upload="handleBeforeUpload"
  10. :on-error="handleError"
  11. :on-success="handleSuccess"
  12. :http-request="upload"
  13. :disabled="disabled"
  14. :show-file-list="false"
  15. style="display: inline-block; margin: 0 10px"
  16. ref="UploadComp"
  17. >
  18. <el-button
  19. :type="btnType"
  20. :icon="btnIcon"
  21. :loading="loading"
  22. :disabled="disabled"
  23. >{{ btnContent }}</el-button
  24. >
  25. </el-upload>
  26. </template>
  27. <script>
  28. import { fileMD5 } from "@/plugins/md5";
  29. import { $httpWithMsg } from "@/plugins/axios";
  30. export default {
  31. name: "UploadButton",
  32. props: {
  33. btnIcon: {
  34. type: String,
  35. },
  36. btnType: {
  37. type: String,
  38. default: "default",
  39. },
  40. btnContent: {
  41. type: String,
  42. },
  43. accept: {
  44. type: String,
  45. },
  46. format: {
  47. type: Array,
  48. default() {
  49. return ["xlsx", "xls"];
  50. },
  51. },
  52. uploadUrl: {
  53. type: String,
  54. required: true,
  55. },
  56. uploadData: {
  57. type: Object,
  58. default() {
  59. return {};
  60. },
  61. },
  62. maxSize: {
  63. type: Number,
  64. default: 20 * 1024 * 1024,
  65. },
  66. addFilenameParam: {
  67. type: String,
  68. default: "filename",
  69. },
  70. disabled: { type: Boolean, default: false },
  71. },
  72. data() {
  73. return {
  74. headers: {
  75. md5: "",
  76. },
  77. res: {},
  78. loading: false,
  79. uploadDataDict: {},
  80. };
  81. },
  82. methods: {
  83. checkFileFormat(fileType) {
  84. const _file_format = fileType.split(".").pop().toLocaleLowerCase();
  85. return this.format.length
  86. ? this.format.some((item) => item.toLocaleLowerCase() === _file_format)
  87. : true;
  88. },
  89. async handleBeforeUpload(file) {
  90. this.uploadDataDict = {
  91. ...this.uploadData,
  92. };
  93. this.uploadDataDict[this.addFilenameParam] = file.name;
  94. if (!this.checkFileFormat(file.name)) {
  95. this.handleFormatError();
  96. return Promise.reject();
  97. }
  98. if (file.size > this.maxSize) {
  99. this.handleExceededSize();
  100. return Promise.reject();
  101. }
  102. const md5 = await fileMD5(file);
  103. this.headers["md5"] = md5;
  104. this.loading = true;
  105. return true;
  106. },
  107. upload(options) {
  108. if (!options.file) return Promise.reject("文件丢失");
  109. let formData = new FormData();
  110. Object.entries(options.data).forEach(([k, v]) => {
  111. formData.append(k, v);
  112. });
  113. formData.append("file", options.file);
  114. this.$emit("uploading");
  115. return $httpWithMsg.post(options.action, formData, {
  116. headers: options.headers,
  117. });
  118. },
  119. handleError(error) {
  120. this.loading = false;
  121. this.res = {
  122. success: false,
  123. message: error.response.data.desc,
  124. };
  125. this.uploadDataDict = {};
  126. this.$refs.UploadComp.clearFiles();
  127. this.$emit("upload-error", error);
  128. },
  129. handleSuccess(res) {
  130. this.loading = false;
  131. this.res = {
  132. success: true,
  133. message: "导入成功!",
  134. };
  135. this.$emit("upload-success", res);
  136. },
  137. handleFormatError() {
  138. const content = "只支持文件格式为" + this.format.join("/");
  139. this.res = {
  140. success: false,
  141. message: content,
  142. };
  143. this.$emit("valid-error", this.res);
  144. },
  145. handleExceededSize() {
  146. const content =
  147. "文件大小不能超过" + Math.floor(this.maxSize / (1024 * 1024)) + "M";
  148. this.res = {
  149. success: false,
  150. message: content,
  151. };
  152. this.$emit("valid-error", this.res);
  153. },
  154. },
  155. };
  156. </script>