UploadFileView.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <div class="upload-file-view">
  3. <el-input
  4. :style="{ width: inputWidth }"
  5. v-model.trim="attachmentName"
  6. placeholder="文件名称"
  7. readonly
  8. ></el-input>
  9. <el-upload
  10. :action="uploadUrl"
  11. :headers="headers"
  12. :max-size="maxSize"
  13. :format="format"
  14. :data="uploadDataDict"
  15. :on-change="handleFileChange"
  16. :before-upload="handleBeforeUpload"
  17. :on-error="handleError"
  18. :on-success="handleSuccess"
  19. :http-request="upload"
  20. :show-file-list="false"
  21. :auto-upload="false"
  22. style="display:inline-block;margin: 0 10px;"
  23. ref="UploadComp"
  24. >
  25. <el-button type="primary">选择</el-button>
  26. </el-upload>
  27. <el-button
  28. type="primary"
  29. @click="startUpload"
  30. v-if="canUpload"
  31. :loading="loading"
  32. style="margin-right: 10px;"
  33. >开始上传</el-button
  34. >
  35. </div>
  36. </template>
  37. <script>
  38. import { fileMD5 } from "@/plugins/md5";
  39. import ajax from "@/plugins/ajax";
  40. export default {
  41. name: "upload-file-view",
  42. props: {
  43. inputWidth: {
  44. type: String,
  45. default: "439px"
  46. },
  47. format: {
  48. type: Array,
  49. default() {
  50. return ["xls", "xlsx"];
  51. }
  52. },
  53. uploadUrl: {
  54. type: String,
  55. required: true
  56. },
  57. uploadData: {
  58. type: Object,
  59. default() {
  60. return {};
  61. }
  62. },
  63. maxSize: {
  64. type: Number,
  65. default: 10 * 1024 * 1024
  66. },
  67. addFilenameParam: {
  68. type: String,
  69. default: "filename"
  70. }
  71. },
  72. data() {
  73. return {
  74. attachmentName: "",
  75. canUpload: false,
  76. loading: false,
  77. uploadDataDict: {},
  78. headers: {
  79. token: this.$ls.get("token"),
  80. md5: ""
  81. },
  82. res: {}
  83. };
  84. },
  85. methods: {
  86. startUpload() {
  87. this.loading = true;
  88. this.$refs.UploadComp.submit();
  89. },
  90. checkFileFormat(fileType) {
  91. const _file_format = fileType
  92. .split(".")
  93. .pop()
  94. .toLocaleLowerCase();
  95. return this.format.some(
  96. item => item.toLocaleLowerCase() === _file_format
  97. );
  98. },
  99. handleFileChange(file) {
  100. this.attachmentName = file.name;
  101. this.canUpload = file.status === "ready";
  102. },
  103. async handleBeforeUpload(file) {
  104. this.uploadDataDict = {
  105. ...this.uploadData,
  106. schoolId: this.$ls.get("schoolId"),
  107. userId: this.$ls.get("user", { id: "" }).id
  108. };
  109. this.uploadDataDict[this.addFilenameParam] = file.name;
  110. if (file.size > this.maxSize) {
  111. this.handleExceededSize();
  112. return Promise.reject();
  113. }
  114. if (!this.checkFileFormat(file.name)) {
  115. this.handleFormatError();
  116. return Promise.reject();
  117. }
  118. const md5 = await fileMD5(file);
  119. this.headers["md5"] = md5;
  120. return true;
  121. },
  122. upload(options) {
  123. return ajax(options);
  124. },
  125. handleError(error) {
  126. this.canUpload = false;
  127. this.loading = false;
  128. this.res = {
  129. success: false,
  130. msg: error.message
  131. };
  132. this.$emit("upload-error", error);
  133. },
  134. handleSuccess(response) {
  135. this.canUpload = false;
  136. this.loading = false;
  137. if (response.code === "200") {
  138. this.res = {
  139. success: true,
  140. msg: "导入成功!"
  141. };
  142. this.$emit("upload-success", response);
  143. } else {
  144. this.handleError(response);
  145. }
  146. },
  147. handleFormatError() {
  148. const content = "只支持文件格式为" + this.format.join("/");
  149. this.res = {
  150. success: false,
  151. msg: content
  152. };
  153. this.loading = false;
  154. this.$emit("upload-error", content);
  155. },
  156. handleExceededSize() {
  157. const content =
  158. "文件大小不能超过" + Math.floor(this.maxSize / 1024) + "M";
  159. this.res = {
  160. success: false,
  161. msg: content
  162. };
  163. this.loading = false;
  164. this.$emit("upload-error", content);
  165. },
  166. setAttachmentName(name) {
  167. this.attachmentName = name;
  168. }
  169. }
  170. };
  171. </script>
  172. <style scoped>
  173. .upload-file-view {
  174. display: inline-block;
  175. }
  176. </style>