UploadButton.vue 3.7 KB

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