UploadButton.vue 3.5 KB

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