UploadFetchFile.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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="upload-url"
  11. :on-change="handleFileChange"
  12. :show-file-list="false"
  13. :auto-upload="false"
  14. :disabled="disabled"
  15. style="display:inline-block;margin: 0 10px;"
  16. ref="UploadComp"
  17. >
  18. <el-button type="primary" :disabled="disabled" :loading="loading"
  19. >选择</el-button
  20. >
  21. </el-upload>
  22. </div>
  23. </template>
  24. <script>
  25. import { fileMD5 } from "@/plugins/md5";
  26. export default {
  27. name: "upload-file-view",
  28. props: {
  29. inputWidth: {
  30. type: String,
  31. default: "200px"
  32. },
  33. format: {
  34. type: Array,
  35. default() {
  36. return ["xls", "xlsx"];
  37. }
  38. },
  39. maxSize: {
  40. type: Number,
  41. default: 20 * 1024 * 1024
  42. },
  43. disabled: {
  44. type: Boolean,
  45. default: false
  46. }
  47. },
  48. data() {
  49. return {
  50. attachmentName: "",
  51. canUpload: false,
  52. loading: false,
  53. res: {}
  54. };
  55. },
  56. methods: {
  57. checkFileFormat(fileType) {
  58. const _file_format = fileType
  59. .split(".")
  60. .pop()
  61. .toLocaleLowerCase();
  62. return this.format.some(
  63. item => item.toLocaleLowerCase() === _file_format
  64. );
  65. },
  66. async handleFileChange(file) {
  67. console.log(file);
  68. this.attachmentName = file.name;
  69. this.canUpload = file.status === "ready";
  70. if (file.size > this.maxSize) {
  71. this.handleExceededSize();
  72. return Promise.reject();
  73. }
  74. if (!this.checkFileFormat(file.name)) {
  75. this.handleFormatError();
  76. return Promise.reject();
  77. }
  78. this.$emit("valid-change", { success: true });
  79. const md5 = await fileMD5(file.raw);
  80. this.$emit("file-change", {
  81. md5,
  82. filename: file.name,
  83. file: file.raw
  84. });
  85. },
  86. // upload(options) {
  87. // let formData = new FormData();
  88. // Object.entries(options.data).forEach(([k, v]) => {
  89. // formData.append(k, v);
  90. // });
  91. // formData.append("file", options.file);
  92. // this.$emit("uploading");
  93. // return $post(options.action, formData, { headers: options.headers });
  94. // },
  95. handleFormatError() {
  96. const content = "只支持文件格式为" + this.format.join("/");
  97. this.res = {
  98. success: false,
  99. message: content
  100. };
  101. this.$emit("valid-change", this.res);
  102. },
  103. handleExceededSize() {
  104. const content =
  105. "文件大小不能超过" + Math.floor(this.maxSize / 1024) + "M";
  106. this.res = {
  107. success: false,
  108. message: content
  109. };
  110. this.$emit("valid-change", this.res);
  111. },
  112. setAttachmentName(name) {
  113. this.attachmentName = name;
  114. }
  115. }
  116. };
  117. </script>
  118. <style scoped>
  119. .upload-file-view {
  120. display: inline-block;
  121. }
  122. </style>