import-dialog.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <!-- 上传 -->
  3. <el-dialog title="导入文件上传" :visible.sync="dialogVisible" width="40%">
  4. <el-form label-width="110px" class="zw-criterion">
  5. <el-form-item label="选择文件">
  6. <el-upload
  7. class="avatar-uploader"
  8. action="#"
  9. :show-file-list="false"
  10. :http-request="handlSuccess"
  11. :before-upload="beforeUpload"
  12. >
  13. <el-button icon="el-icon-plus" size="small" type="primary"
  14. >文件上传</el-button
  15. >
  16. <div slot="tip" class="el-upload__tip">
  17. 只能上传excel文件,点击
  18. <el-link
  19. type="primary"
  20. :underline="false"
  21. @click="downLoadTemplate()"
  22. >
  23. 下载模板</el-link
  24. >
  25. </div>
  26. </el-upload>
  27. </el-form-item>
  28. <el-form-item label="上传列表">
  29. <div class="imgs-box">
  30. <p v-for="(item, index) in attaments" :key="index" class="imgs-p">
  31. <span> {{ item.name }}</span>
  32. <el-link @click="delFileList(index)" type="primary">删除</el-link>
  33. </p>
  34. </div>
  35. </el-form-item>
  36. </el-form>
  37. <div slot="footer" class="dialog-footer">
  38. <el-button size="small" @click="dialogVisible = false">关 闭</el-button>
  39. <el-button size="small" @click="upload" type="primary">上 传</el-button>
  40. </div>
  41. </el-dialog>
  42. </template>
  43. <script>
  44. import { importBatch } from '@/api/system/file/index.js';
  45. import { importFile, importTemplate } from '@/api/inspectionProject';
  46. import { download1 } from '@/utils/file';
  47. export default {
  48. props: {
  49. // eslint-disable-next-line vue/require-prop-type-constructor
  50. defModule: '',
  51. fileUrl: '',
  52. fileName: '',
  53. apiUrl: ''
  54. },
  55. //注册组件
  56. data() {
  57. return {
  58. showViewer: false, // 显示查看器
  59. dialogVisible: false,
  60. uploadShow: false,
  61. module: '',
  62. attaments: [], //上传文件
  63. file: ''
  64. };
  65. },
  66. created() {},
  67. methods: {
  68. open() {
  69. this.attaments = [];
  70. this.module = '';
  71. this.dialogVisible = true;
  72. },
  73. //删除附件
  74. delFileList(index) {
  75. this.attaments.splice(index, 1);
  76. },
  77. //上传限制
  78. beforeUpload(file) {
  79. const isLt10M = file.size / 1024 / 1024 < 10;
  80. if (!isLt10M) {
  81. this.$message.error('导入单文件大小不能超过 10MB!');
  82. }
  83. return isLt10M;
  84. },
  85. //图片上传
  86. handlSuccess(param) {
  87. this.file = param.file;
  88. this.attaments.push(param.file);
  89. },
  90. // 文件上传
  91. async upload() {
  92. if (this.attaments.length == 0) {
  93. return this.$message.warning('文件不能为空!');
  94. }
  95. const formData = new FormData();
  96. this.attaments.forEach((item, index) => {
  97. formData.append(`importExcels`, item);
  98. });
  99. this.loading = true;
  100. importFile(formData)
  101. .then((res) => {
  102. this.$message.success('操作成功!');
  103. this.dialogVisible = false;
  104. this.$emit('success');
  105. this.loading = false;
  106. })
  107. .catch((data) => {
  108. this.loading = false;
  109. if (data.code != -1) {
  110. // this.$message.info(data.message);
  111. this.$alert(data.message, '提示', {
  112. confirmButtonText: '确定',
  113. callback: (action) => {}
  114. });
  115. }
  116. });
  117. },
  118. downLoadTemplate() {
  119. download1(window.location.origin + this.fileUrl, this.fileName);
  120. // var a = document.createElement('a'); //创建一个<a></a>标签
  121. // a.href = 'static/model.xlsx'; // 给a标签的href属性值加上地址,注意,这里是绝对路径,不用加 点.
  122. // a.download = '质检项导入模板.xlsx'; //设置下载文件文件名,这里加上.xlsx指定文件类型,pdf文件就指定.fpd即可
  123. // a.style.display = 'none'; // 障眼法藏起来a标签
  124. // document.body.appendChild(a); // 将a标签追加到文档对象中
  125. // a.click(); // 模拟点击了a标签,会触发a标签的href的读取,浏览器就会自动下载了
  126. // a.remove();
  127. }
  128. }
  129. };
  130. </script>
  131. <style lang="scss">
  132. .zw-table-header {
  133. float: right;
  134. }
  135. .imgs-box .imgs-p {
  136. height: 30px;
  137. background: #f0f3f3;
  138. line-height: 30px;
  139. width: 372px;
  140. margin-bottom: 5px;
  141. padding: 0 10px;
  142. display: flex;
  143. justify-content: space-between;
  144. }
  145. .zw-criterion-normal {
  146. padding: 20px 0 0 0;
  147. }
  148. .el-main {
  149. overflow: hidden;
  150. }
  151. </style>