您现在的位置是:首页 > 编程语言学习 > 前端编程语言 > 文章正文 前端编程语言

elementui之封装下载模板和导入文件组件方式

2022-12-07 11:14:02 前端编程语言

简介这篇文章主要介绍了关于elementui之封装下载模板和导入文件组件方式,具有很好的参考价值,希望对大家有所帮助。elementui封装下载模板和上...

这篇文章主要介绍了关于elementui之封装下载模板和导入文件组件方式,具有很好的参考价值,希望对大家有所帮助。

elementui封装下载模板和上传组件

封装代码如下

  1. <template> 
  2.   <div> 
  3. <el-dialog 
  4.   :title="exportStatus? '导入结果' : '一键导入数据'" 
  5.   :visible.sync="dialogVisible" 
  6.   width="905px" 
  7.   :before-close="handleClose" 
  8.   class="export-standard-speech" 
  9. <div class="standard-export" v-if="!exportStatus"
  10.   <div class="export-title">1、下载导入模板</div> 
  11.   <div class="export-warning">根据提示完善表格内容</div> 
  12.   <div class="export-download-btn"
  13. <img 
  14.   src="@/assets/img/download-demo-icon.png" 
  15.   width="12px" 
  16.   style=" 
  17. vertical-align: baseline; 
  18. margin-right: 3px; 
  19. margin-top:2px; 
  20. display: inline-block; 
  21.   " 
  22. /> 
  23. <a 
  24.   class="a-line" 
  25.   :href="downloadDemoUrl" rel="external nofollow"  
  26.   download="项目导入模板.xlsx" 
  27.   title="下载空的表格模板" 
  28. >下载空的表格模板</a> 
  29.   </div> 
  30.   <div class="export-title">2、上传完善后的表格</div> 
  31.   <el-upload 
  32. class="upload-demo-xls" 
  33. drag 
  34. ref="speechDemoUpload" 
  35. action="" 
  36. :file-list="fileList" 
  37. :auto-upload="false" 
  38. :show-file-list="false" 
  39. accept=".xls,.xlsx,.csv" 
  40. :on-change="handleChange" 
  41.   > 
  42. <div class="el-upload__text" v-if="!uploadStatus">将文件拖到此处,或点击<em>上传</em></div> 
  43. <div class="el-upload__text" v-else
  44.   <span class="file-name">{{ this.file && this.file.name }}</span> 
  45.   <span class="file-size">({{bytesToSize(this.file.size)}})</span> 
  46.   <span class="file-select">重新选择</span> 
  47. </div> 
  48.   </el-upload> 
  49.   <div class="h32 flex-center-center" style="margin-top:38px"
  50. <Button text="取消" background="#fff" border="#4646E6" color="#4646E6" class="ml10"  @click.native="cancelExport" ></Button> 
  51. <Button text="导入" background="#4646E6" color="#fff" class="ml10"  @click.native="xlsDemoExport" :setGray="isFile"></Button> 
  52.   </div> 
  53. </div> 
  54. <div v-if="exportStatus && failNum > 0"
  55.   <div class="fail-num"
  56. <img src="@/assets/img/export-fail-img.png" alt="" class="mr10"
  57. 导入失败 
  58. <span>{{failNum}}条</span> 
  59. 数据 
  60.   </div> 
  61.   <div class="export-fail-table"
  62. <div class="export-fail-text">以下为导入失败的项目,您可修改后重新导入</div> 
  63. <el-table 
  64.   :data="tableData" 
  65.   height="220" 
  66.   <el-table-column 
  67. prop="lineNum" 
  68. label="Execel行数" 
  69. width="220" 
  70. align="left" 
  71.   > 
  72.   </el-table-column> 
  73.   <el-table-column 
  74. prop="projectCode" 
  75. label="项目编号" 
  76. width="220" 
  77. align="center" 
  78.   > 
  79.   </el-table-column> 
  80.   <el-table-column 
  81. prop="failReason" 
  82. label="失败原因" 
  83. width="220" 
  84. align="right" 
  85.   > 
  86.   </el-table-column> 
  87. </el-table> 
  88.   </div> 
  89.   <div class="h32 flex-center-center" style="margin-top:23px"
  90. <Button text="关闭" background="#fff" border="#4646E6" color="#4646E6"   @click.native="cancelExport" ></Button> 
  91.   </div> 
  92. </div> 
  93. <div v-if="exportStatus && failNum == 0"
  94.   <div class="success-img"
  95. <img src="@/assets/img/export-success-img.png" alt="" class="mr10"
  96.   </div> 
  97.   <div class="success-text"
  98. 成功导入 
  99. <span>{{ successNum }}条</span> 
  100. 数据 
  101.   </div> 
  102.   <div class="h32 flex-center-center" style="margin-top:23px"
  103. <Button text="完成" background="#4646E6" color="#fff" class=""  @click.native="cancelExport"></Button> 
  104.   </div> 
  105. </div> 
  106. </el-dialog> 
  107.   </div> 
  108. </template> 
  109.  
  110. <script> 
  111. import Button from "@/components/Button"
  112. import { upload, service } from '@/utils/request-brand' 
  113. import { bytesToSize } from '@/utils' 
  114.  
  115. export default { 
  116.   name: 'ImportData'
  117.   components: { 
  118. Button 
  119.   }, 
  120.   props: { 
  121. showDialogVisible: { 
  122.   type: Boolean, 
  123.   defaultfalse
  124.   required: true 
  125. }, 
  126. downloadDemoUrl:{ 
  127.   type: String, 
  128.   required: true 
  129. }, 
  130. uploadFileUrl:{ 
  131.   type: String, 
  132.   required: true 
  133. }, 
  134. // 考虑上传时其他参数的情况 
  135. otherUploadData:{ 
  136.   type: Object, 
  137.   default:() => { 
  138. return {} 
  139.   } 
  140.   }, 
  141.   computed: { 
  142. userAccountID() { 
  143.   return this.$store.state.user.userAccountID; 
  144. }, 
  145. isFile(){ 
  146.   if(this.file){ 
  147. return false 
  148.   } else { 
  149. return true 
  150.   } 
  151.   }, 
  152.   watch: { 
  153. showDialogVisible(newVal) { 
  154.   // console.log(newVal); 
  155.   this.dialogVisible = newVal 
  156.   }, 
  157.   data () { 
  158. return { 
  159.   dialogVisible: false
  160.   fileList: [], 
  161.   file: null
  162.   // 上传文件的状态切换 
  163.   uploadStatus: false
  164.   successNum: 0, 
  165.   failNum: 0, 
  166.   tableData: [], 
  167.   // 导入的状态 
  168.   exportStatus: false
  169. }; 
  170.   }, 
  171.   methods: { 
  172. bytesToSize, 
  173. handleClose(done) { 
  174.   this.dialogVisible = false 
  175.   this.$emit('dialogVisible'false
  176. }, 
  177. // 取消或者清空上传的文件和数据 
  178. cancelExport(){ 
  179.   // this.$refs.speechDemoUpload.clearFiles() 
  180.   this.fileList = [] 
  181.   this.file = null 
  182.   this.successNum = 0 
  183.   this.failNum = 0 
  184.   this.tableData = [] 
  185.   this.uploadStatus = false 
  186.   this.exportStatus = false 
  187.   this.$emit('dialogVisible'false
  188. }, 
  189. // 上传的文件改变时(覆盖原来的文件) 
  190. handleChange(file, fileList){ 
  191.   // console.log(file); 
  192.   let extension = file.raw.name.substring(file.raw.name.lastIndexOf(".") + 1); 
  193.   let size = file.size / 1024 / 1024; 
  194.   // let size = file.size / 1024; 
  195.   // console.log(extension, extension.toLowerCase() !== "xlsx"); 
  196.   if (!['xlsx','xls','csv'].includes(extension.toLowerCase())) { 
  197. this.$message.warning("文件格式不正确,请上传xls / xlsx / csv格式"); 
  198. return false
  199.   } 
  200.   if (size > 10) { 
  201. this.$message.warning("文件过大,请进行拆分后分多次上传"); 
  202. return false 
  203.   } 
  204.   // console.log(file.raw, fileList); 
  205.   if (fileList.length > 0) { 
  206. this.fileList = [fileList[fileList.length - 1]]  // 这一步,是展示最后一次选择的csv文件 
  207. this.file = this.fileList[0].raw 
  208.   } 
  209.   // console.log(this.file); 
  210.   this.uploadStatus = true 
  211. }, 
  212. async xlsDemoExport(){ 
  213.   if (!this.file) { 
  214. // return this.$message.error('请上传文件') 
  215. return false 
  216.   } 
  217.   const formData = new FormData() 
  218.   formData.append('file'this.file) 
  219.   // 添加其他参数 
  220.   if(Object.values(this.otherUploadData).length > 0){ 
  221. for (let key in this.otherUploadData) { 
  222.   formData.append(key, this.otherUploadData[key]) 
  223.   } 
  224.   //  调用上传接口... 
  225.   this.$loading.show() 
  226.   // `${process.env.VUE_APP_BASE_BRAND_API}/v1/inspectionSpeechArt/importExcel 
  227.   upload.uploadFile(this.uploadFileUrl, formData).then(res => { 
  228. if(res.code == 0){ 
  229.   this.exportStatus = true 
  230.   this.tableData = res.item.failList.map(item => { 
  231. let obj = {} 
  232. obj.lineNum = item.lineNum 
  233. obj.failReason = item .failReason 
  234. obj.projectCode = item. projectCode 
  235. return obj 
  236.   }) 
  237.   this.failNum  = res.item.failNum 
  238.   this.successNum = res.item.successNum 
  239. else { 
  240.   this.$message.error(res.message) 
  241. this.$loading.hide() 
  242.   }).catch((error) => { 
  243. console.log(error); 
  244. this.$message.error('上传失败,请稍后再试或联系IT解决'
  245. this.$loading.hide() 
  246.   }) 
  247. }, 
  248.   }, 
  249. </script> 
  250.  
  251. <style  lang="scss" scoped> 
  252. ::v-deep .el-dialog{ 
  253.   width: 25%; 
  254.   border-radius: 12px; 
  255.   margin-top:-8vh !important; 
  256.   top: 50%; 
  257.   transform: translateY(-50%); 
  258. ::v-deep .el-dialog__header { 
  259.   border-bottom: 1px solid #efeff6; 
  260.   padding-left: 50px; 
  261.   .el-dialog__title, 
  262.   .el-dialog__headerbtn i { 
  263. color: #36395c !important; 
  264. font-size: 18px !important; 
  265. font-weight: 600; 
  266.   } 
  267. </style> 
  268.  
  269. <style lang="scss"
  270. .export-standard-speech { 
  271.   .el-list-enter-active, 
  272.   .el-list-leave-active { 
  273. transition: none; 
  274.   } 
  275.   .el-list-enter, 
  276.   .el-list-leave-active { 
  277. opacity: 0; 
  278.   } 
  279.   .el-upload-list { 
  280. height: 40px; 
  281.   } 
  282.   // 表格body 
  283.   .el-dialog__body{ 
  284. padding: 30px 100px; 
  285.   } 
  286.   // 导入 
  287.   .standard-export
  288. .upload-demo-xls{ 
  289.   width: 100%; 
  290.   margin-top: 20px; 
  291. .el-upload.el-upload--text{ 
  292.   width: 100%; 
  293. .el-upload-dragger{ 
  294.   width: 100%; 
  295.   display: flex; 
  296.   justify-content: center; 
  297.   align-items: center; 
  298.   border: 1px solid #EFEFF6; 
  299.   height: 120px; 
  300.   .el-upload__text{ 
  301. font-weight: 600; 
  302. color: #36395C; 
  303. .file-name{ 
  304.   color: #4646E6; 
  305. .file-size{ 
  306.   color: #CCCDD8; 
  307. .file-select{ 
  308.   color: #4646E6; 
  309.   text-decoration: underline; 
  310.   } 
  311. .export-title{ 
  312.   font-size: 14px; 
  313.   color: #36395C; 
  314.   font-weight: 600; 
  315.   line-height: 20px; 
  316. .export-warning{ 
  317.   font-size: 14px; 
  318.   color: #CCCDD8; 
  319.   line-height: 20px; 
  320. .export-download-btn{ 
  321.   width: 165px; 
  322.   height: 36px; 
  323.   border: 1px solid #4646E6; 
  324.   color: #4646E6; 
  325.   opacity: 1; 
  326.   border-radius: 6px; 
  327.   display: flex; 
  328.   align-items: center; 
  329.   justify-content: center; 
  330.   margin: 20px 0 30px; 
  331.   } 
  332.  
  333.   // 导入失败的表格数据 
  334.   .fail-num{ 
  335. display: flex; 
  336. justify-content: center; 
  337. align-items: center; 
  338. margin-bottom: 24px; 
  339. font-size: 16px; 
  340. color: #36395C; 
  341. font-weight: 600; 
  342. span{ 
  343.   color: #EF6E49; 
  344.   margin: 0 3px; 
  345.   } 
  346.   .export-fail-table{ 
  347. width: 100%; 
  348. height: 280px; 
  349. border: 1px solid #EFEFF6; 
  350. border-radius: 6px; 
  351. padding: 0 20px; 
  352. .export-fail-text{ 
  353.   font-size: 14px; 
  354.   line-height: 20px; 
  355.   color: #36395C; 
  356.   font-weight: 600; 
  357.   margin-bottom: 20px; 
  358.   margin-top: 20px; 
  359. .el-table__row { 
  360.   height: 60px; 
  361.   td { 
  362. padding: 0 30px; 
  363.   } 
  364. .has-gutter tr { 
  365.   height: 60px; 
  366. .has-gutter tr th { 
  367.   font-size: 14px; 
  368.   color: #333333; 
  369.   font-family: Arial; 
  370.   padding: 0 20px; 
  371. thead th { 
  372.   font-size: 14px; 
  373.   color: #333333; 
  374.   font-weight: 600; 
  375.   height: 40px; 
  376.   background: #F5F6FB; 
  377. // 滚动条的宽度 
  378. .el-table__body-wrapper::-webkit-scrollbar { 
  379.   width: 6px; // 横向滚动条 
  380.   height: 6px; // 纵向滚动条 
  381. // 滚动条的滑块 
  382. .el-table__body-wrapper::-webkit-scrollbar-thumb { 
  383.   background-color: #E2E2E2; 
  384.   border-radius: 3px; 
  385.   } 
  386.   .success-img{ 
  387. display: flex; 
  388. justify-content: center; 
  389. align-items: center; 
  390. margin-top: 103px; 
  391.   } 
  392.   .success-text{ 
  393. display: flex; 
  394. justify-content: center; 
  395. align-items: center; 
  396. margin-top: 30px; 
  397. margin-bottom: 50px; 
  398. font-size: 16px; 
  399. color: #36395C; 
  400. font-weight: 600; 
  401. span{ 
  402.   color: #4646E6; 
  403.   margin: 0 3px; 
  404.   } 
  405. </style> 

页面使用

只上传文件的形式

  1. <template> 
  2.   <div class="upload"
  3.     <Button  text="批量导入"  background="#36395C" color="#fff" :icon="true"  @click.native="handleExportBtn" /> 
  4.  
  5. // ... 
  6. <ImportData 
  7.   :showDialogVisible="exportVisible" 
  8.   :downloadDemoUrl="downloadStandardSpeech" 
  9.   :uploadFileUrl="uploadFileUrl" 
  10.   @dialogVisible="closeVisible" 
  11. /> 
  12.   </div> 
  13. </template> 
  14.  
  15. <script> 
  16. import ImportData from '@/views/components/ImportData.vue' 
  17.  
  18. export default { 
  19.   name: "index"
  20.   components: { 
  21. ImportData, 
  22.   }, 
  23.   data() { 
  24. return { 
  25.   // 导入弹框显示和隐藏 
  26.   exportVisible: false
  27.   downloadStandardSpeech: `${process.env.VUE_APP_EXPORTDEOM_API}/fileResources/templates_standardSpeech.xlsx`, // 模板下载文件地址, 
  28.   uploadFileUrl: 'http://10.0.0.10:3000/v1/inspectionSpeechArt/importExcel' // 可以写在配置文件中 
  29. }; 
  30.   }, 
  31.   computed: { 
  32. userAccountID() { 
  33.   return this.$store.state.user.userAccountID; 
  34. }, 
  35.   }, 
  36.   // created() { 
  37.   //   this.getDataList(); 
  38.   //   this.getCheckType(); 
  39.   // }, 
  40.   activated() { 
  41. this.getDataList(); 
  42. this.getCheckType(); 
  43.   }, 
  44.   watch:{}, 
  45.   methods: { 
  46. // 导入 
  47. handleExportBtn(){ 
  48.   this.exportVisible = true 
  49. }, 
  50. // 关闭导入 
  51. closeVisible(){ 
  52.   this.exportVisible = false 
  53.   // 刷新列表等操作 
  54. }, 
  55.   }, 
  56. }; 
  57. </script> 
  58.  
  59.  
  60. <style lang="scss" scoped> 
  61. </style> 

上传文件和其他必传参数的形式

  1. <template> 
  2.   <div class="upload"
  3.     <Button  text="批量导入"  background="#36395C" color="#fff" :icon="true"  @click.native="handleExportBtn" /> 
  4.  
  5. // ... 
  6. <ImportData 
  7.   :showDialogVisible="exportVisible" 
  8.   :downloadDemoUrl="downloadStandardSpeech" 
  9.   :uploadFileUrl="uploadFileUrl" 
  10.   :otherUploadData="otherUploadData" 
  11.   @dialogVisible="closeVisible" 
  12. /> 
  13.   </div> 
  14. </template> 
  15.  
  16. <script> 
  17. import ImportData from '@/views/components/ImportData.vue' 
  18.  
  19. export default { 
  20.   name: "index"
  21.   components: { 
  22. ImportData, 
  23.   }, 
  24.   data() { 
  25. return { 
  26.   // 导入弹框显示和隐藏 
  27.   exportVisible: false
  28.   downloadStandardSpeech: `${process.env.VUE_APP_EXPORTDEOM_API}/fileResources/templates_standardSpeech.xlsx`, // 模板下载文件地址, 
  29.   uploadFileUrl: 'http://10.0.0.10:3000/v1/inspectionSpeechArt/importExcel'
  30.   otherUploadData: {}, 
  31. }; 
  32.   }, 
  33.   computed: { 
  34. userAccountID() { 
  35.   return this.$store.state.user.userAccountID; 
  36. }, 
  37.   }, 
  38.   // created() { 
  39.   //   this.getDataList(); 
  40.   //   this.getCheckType(); 
  41.   //   this.otherUploadData = { key : 'value' }; 
  42.   // }, 
  43.   activated() { 
  44. this.getDataList(); 
  45. this.getCheckType(); 
  46. this.otherUploadData = { key : 'value' }; 
  47.   }, 
  48.   watch:{}, 
  49.   methods: { 
  50. // 导入 
  51. handleExportBtn(){ 
  52.   this.exportVisible = true 
  53. }, 
  54. // 关闭导入 
  55. closeVisible(){ 
  56.   this.exportVisible = false 
  57.   // 刷新列表等操作 
  58. }, 
  59.   }, 
  60. }; 
  61. </script> 
  62.  
  63.  
  64. <style lang="scss" scoped> 
  65. </style> 

效果如下

相关文章

站点信息