UploadButton.vue 3.1 KB

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