ImportFile.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <div :class="prefixCls">
  3. <el-dialog
  4. class="modify-data"
  5. :visible.sync="modalIsShow"
  6. :title="title"
  7. top="10vh"
  8. width="500px"
  9. :close-on-click-modal="false"
  10. :close-on-press-escape="false"
  11. append-to-body
  12. @open="visibleChange"
  13. >
  14. <div style="footer"></div>
  15. <div :class="[`${prefixCls}-footer`]" v-if="downloadUrl">
  16. 文件下载:
  17. <a :href="downloadUrl" :download="dfilename">{{ dfilename }}</a>
  18. </div>
  19. <div :class="[`${prefixCls}-footer`]" v-if="downloadHandle">
  20. 文件下载:
  21. <el-button type="text" @click="downloadHandle">{{
  22. dfilename
  23. }}</el-button>
  24. </div>
  25. <div :class="[`${prefixCls}-body`]">
  26. <el-upload
  27. drag
  28. :action="uploadUrl"
  29. :headers="headers"
  30. :max-size="maxSize"
  31. :format="format"
  32. :accept="accept"
  33. :data="uploadDataDict"
  34. :before-upload="handleBeforeUpload"
  35. :on-error="handleError"
  36. :on-success="handleSuccess"
  37. :http-request="upload"
  38. :disabled="disabled || loading"
  39. :show-file-list="false"
  40. ref="UploadComp"
  41. >
  42. <i class="el-icon-upload"></i><br />
  43. <em class="el-upload__text">将文件拖到此处,或<em>点击上传</em></em>
  44. <p
  45. slot="tip"
  46. :class="[
  47. `${prefixCls}-tips`,
  48. {
  49. 'cc-tips-success': res.success,
  50. 'cc-tips-error': !res.success
  51. }
  52. ]"
  53. v-if="res.msg"
  54. >
  55. {{ res.msg }}
  56. </p>
  57. </el-upload>
  58. </div>
  59. </el-dialog>
  60. </div>
  61. </template>
  62. <script>
  63. import { fileMD5 } from "@/plugins/md5";
  64. import { $post } from "@/plugins/axios";
  65. const prefixCls = "cc-import-file";
  66. export default {
  67. name: "import-file",
  68. props: {
  69. title: {
  70. type: String,
  71. default: "文件上传"
  72. },
  73. downloadHandle: {
  74. type: Function
  75. },
  76. downloadUrl: {
  77. type: String,
  78. default: ""
  79. },
  80. downloadFilename: String,
  81. format: {
  82. type: Array,
  83. default() {
  84. return ["jpg", "jpeg", "png"];
  85. }
  86. },
  87. uploadUrl: {
  88. type: String,
  89. required: true
  90. },
  91. uploadData: {
  92. type: Object,
  93. default() {
  94. return {};
  95. }
  96. },
  97. maxSize: {
  98. type: Number,
  99. default: 20 * 1024 * 1024
  100. },
  101. addFilenameParam: {
  102. type: String,
  103. default: "filename"
  104. },
  105. disabled: { type: Boolean, default: false }
  106. },
  107. data() {
  108. return {
  109. prefixCls,
  110. modalIsShow: false,
  111. headers: {
  112. md5: ""
  113. },
  114. loading: false,
  115. uploadDataDict: {},
  116. res: {
  117. success: true,
  118. msg: ""
  119. }
  120. };
  121. },
  122. computed: {
  123. dfilename() {
  124. return this.downloadFilename || this.downloadUrl.split("/").pop();
  125. },
  126. accept() {
  127. return this.format.map(el => `.${el}`).join();
  128. }
  129. },
  130. methods: {
  131. visibleChange() {
  132. this.res = {
  133. success: true,
  134. msg: ""
  135. };
  136. this.headers = { md5: "" };
  137. this.loading = false;
  138. this.uploadDataDict = {};
  139. },
  140. checkFileFormat(fileType) {
  141. const _file_format = fileType
  142. .split(".")
  143. .pop()
  144. .toLowerCase();
  145. return this.format.length
  146. ? this.format.some(item => item.toLowerCase() === _file_format)
  147. : true;
  148. },
  149. async handleBeforeUpload(file) {
  150. this.uploadDataDict = {
  151. ...this.uploadData
  152. };
  153. this.uploadDataDict[this.addFilenameParam] = file.name;
  154. if (file.size > this.maxSize) {
  155. this.handleExceededSize();
  156. return Promise.reject();
  157. }
  158. if (!this.checkFileFormat(file.name)) {
  159. this.handleFormatError();
  160. return Promise.reject();
  161. }
  162. const md5 = await fileMD5(file);
  163. this.headers["md5"] = md5;
  164. this.loading = true;
  165. return true;
  166. },
  167. upload(options) {
  168. let formData = new FormData();
  169. Object.entries(options.data).forEach(([k, v]) => {
  170. formData.append(k, v);
  171. });
  172. formData.append("file", options.file);
  173. this.$emit("uploading");
  174. return $post(options.action, formData, { headers: options.headers });
  175. },
  176. handleError(error) {
  177. this.loading = false;
  178. this.res = {
  179. success: false,
  180. msg: error.message
  181. };
  182. this.$emit("upload-error", error);
  183. },
  184. handleSuccess() {
  185. this.loading = false;
  186. this.res = {
  187. success: true,
  188. msg: "导入成功!"
  189. };
  190. this.$emit("upload-success", response);
  191. this.cancel();
  192. },
  193. handleFormatError() {
  194. this.res = {
  195. success: false,
  196. msg: "只支持文件格式为" + this.format.join("/")
  197. };
  198. this.$refs.UploadComp.clearFiles();
  199. },
  200. handleExceededSize() {
  201. this.res = {
  202. success: false,
  203. msg: "文件大小不能超过" + Math.floor(this.maxSize / (1024 * 1024)) + "M"
  204. };
  205. this.$refs.UploadComp.clearFiles();
  206. },
  207. cancel() {
  208. this.modalIsShow = false;
  209. },
  210. open() {
  211. this.modalIsShow = true;
  212. }
  213. }
  214. };
  215. </script>