UploadButton.vue 3.2 KB

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