UploadButton.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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
  76. .split(".")
  77. .pop()
  78. .toLocaleLowerCase();
  79. return this.format.length
  80. ? this.format.some(item => item.toLocaleLowerCase() === _file_format)
  81. : true;
  82. },
  83. async handleBeforeUpload(file) {
  84. this.uploadDataDict = {
  85. ...this.uploadData
  86. };
  87. this.uploadDataDict[this.addFilenameParam] = file.name;
  88. if (file.size > this.maxSize) {
  89. this.handleExceededSize();
  90. return Promise.reject();
  91. }
  92. if (!this.checkFileFormat(file.name)) {
  93. this.handleFormatError();
  94. return Promise.reject();
  95. }
  96. const md5 = await fileMD5(file);
  97. this.headers["md5"] = md5;
  98. this.loading = true;
  99. return true;
  100. },
  101. upload(options) {
  102. return ajax(options);
  103. },
  104. handleError(error) {
  105. this.loading = false;
  106. this.res = {
  107. success: false,
  108. message: error.message
  109. };
  110. this.$emit("upload-error", error);
  111. },
  112. handleSuccess(response) {
  113. this.loading = false;
  114. if (response.code === "200") {
  115. this.res = {
  116. success: true,
  117. message: "导入成功!"
  118. };
  119. this.$emit("upload-success", response);
  120. } else {
  121. this.handleError(response);
  122. }
  123. },
  124. handleFormatError() {
  125. const content = "只支持文件格式为" + this.format.join("/");
  126. this.res = {
  127. success: false,
  128. message: content
  129. };
  130. this.$emit("upload-error", this.res);
  131. },
  132. handleExceededSize() {
  133. const content =
  134. "文件大小不能超过" + Math.floor(this.maxSize / (1024 * 1024)) + "M";
  135. this.res = {
  136. success: false,
  137. message: content
  138. };
  139. this.$emit("upload-error", this.res);
  140. }
  141. }
  142. };
  143. </script>