UploadButton.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. :on-error="handleError"
  10. :on-success="handleSuccess"
  11. :on-change="fileChange"
  12. :http-request="upload"
  13. :disabled="disabled || loading"
  14. :auto-upload="autoUpload"
  15. :show-file-list="false"
  16. style="display: inline-block"
  17. ref="UploadComp"
  18. >
  19. <el-button
  20. :type="btnType"
  21. :icon="btnIcon"
  22. :loading="loading"
  23. :disabled="disabled || loading"
  24. >{{ btnContent }}</el-button
  25. >
  26. </el-upload>
  27. </template>
  28. <script>
  29. import { fileMD5 } from "@/plugins/md5";
  30. import { $httpWithMsg } from "@/plugins/axios";
  31. export default {
  32. name: "UploadButton",
  33. props: {
  34. inputWidth: {
  35. type: String,
  36. default: "203px",
  37. },
  38. btnIcon: {
  39. type: String,
  40. },
  41. btnType: {
  42. type: String,
  43. default: "default",
  44. },
  45. btnContent: {
  46. type: String,
  47. },
  48. accept: {
  49. type: String,
  50. },
  51. format: {
  52. type: Array,
  53. default() {
  54. return ["xlsx", "xls"];
  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. addFileParam: {
  72. type: String,
  73. default: "file",
  74. },
  75. addFilenameParam: {
  76. type: String,
  77. default: "filename",
  78. },
  79. autoUpload: { type: Boolean, default: true },
  80. disabled: { type: Boolean, default: false },
  81. },
  82. data() {
  83. return {
  84. headers: {
  85. md5: "",
  86. },
  87. res: {},
  88. loading: false,
  89. uploadDataDict: {},
  90. filename: "",
  91. fileValid: false,
  92. };
  93. },
  94. methods: {
  95. checkFileFormat(fileType) {
  96. const _file_format = fileType.split(".").pop().toLocaleLowerCase();
  97. return this.format.length
  98. ? this.format.some((item) => item.toLocaleLowerCase() === _file_format)
  99. : false;
  100. },
  101. async fileChange(fileObj, fileList) {
  102. if (fileObj.status === "ready") {
  103. if (fileList.length > 1) {
  104. fileList[0] = fileList[1];
  105. fileList.splice(1, 1);
  106. }
  107. let res = await this.handleBeforeUpload(fileObj.raw).catch(() => {});
  108. if (res) {
  109. this.submit();
  110. }
  111. }
  112. },
  113. async handleBeforeUpload(file) {
  114. this.res = {};
  115. this.filename = file.name;
  116. this.uploadDataDict = {
  117. ...this.uploadData,
  118. };
  119. this.uploadDataDict[this.addFilenameParam] = file.name;
  120. if (!this.checkFileFormat(file.name)) {
  121. this.handleFormatError();
  122. this.fileValid = false;
  123. // return Promise.reject();
  124. return false;
  125. }
  126. if (file.size > this.maxSize) {
  127. this.handleExceededSize();
  128. this.fileValid = false;
  129. // return Promise.reject();
  130. return false;
  131. }
  132. const md5 = await fileMD5(file);
  133. this.headers["md5"] = md5;
  134. this.fileValid = true;
  135. if (this.autoUpload) {
  136. this.loading = true;
  137. return true;
  138. }
  139. return true;
  140. },
  141. submit() {
  142. if (!this.fileValid) {
  143. this.$message.error("文件错误,请重新选择文件!");
  144. return;
  145. }
  146. this.loading = true;
  147. this.$refs.UploadComp.submit();
  148. },
  149. upload(options) {
  150. if (!options.file) return Promise.reject("文件丢失");
  151. let formData = new FormData();
  152. Object.entries(options.data).forEach(([k, v]) => {
  153. formData.append(k, v);
  154. });
  155. formData.append(this.addFileParam, options.file);
  156. this.$emit("uploading");
  157. return $httpWithMsg.post(options.action, formData, {
  158. headers: options.headers,
  159. });
  160. },
  161. handleError(error) {
  162. this.loading = false;
  163. this.res = {
  164. success: false,
  165. message: error.response.data.message,
  166. };
  167. this.uploadDataDict = {};
  168. this.filename = "";
  169. this.fileValid = false;
  170. this.$refs.UploadComp.clearFiles();
  171. this.$emit("upload-error", error.response);
  172. },
  173. handleSuccess(res) {
  174. this.loading = false;
  175. this.res = {
  176. success: true,
  177. message: "导入成功!",
  178. };
  179. this.$emit("upload-success", res);
  180. },
  181. handleFormatError() {
  182. const content = "只支持文件格式为" + this.format.join("/");
  183. this.res = {
  184. success: false,
  185. message: content,
  186. };
  187. this.$emit("valid-error", this.res);
  188. },
  189. handleExceededSize() {
  190. const content =
  191. "文件大小不能超过" + Math.floor(this.maxSize / (1024 * 1024)) + "M";
  192. this.res = {
  193. success: false,
  194. message: content,
  195. };
  196. this.$emit("valid-error", this.res);
  197. },
  198. },
  199. };
  200. </script>