ExamStudentImportDialog.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <el-dialog
  3. ref="dialog"
  4. :title="'导入' + '考生'"
  5. width="450px"
  6. :visible.sync="visible"
  7. @close="closeDialog"
  8. >
  9. <el-form
  10. :model="form"
  11. ref="form"
  12. :rules="rules"
  13. label-position="right"
  14. label-width="120px"
  15. >
  16. <el-row>
  17. <el-form-item label="选择文件">
  18. <input @change="selectFile" type="file" />
  19. </el-form-item>
  20. </el-row>
  21. <el-row class="d-flex justify-content-center">
  22. <el-button type="primary" @click="submitForm" :loading="loading"
  23. >导入</el-button
  24. >
  25. <el-button @click="closeDialog">取消</el-button>
  26. </el-row>
  27. </el-form>
  28. </el-dialog>
  29. </template>
  30. <script>
  31. import { importExamStudent } from "@/api/examwork-task";
  32. import MD5 from "js-md5";
  33. export default {
  34. name: "ExamStudentImportDialog",
  35. props: {
  36. examId: String,
  37. examStudent: Object,
  38. },
  39. computed: {
  40. isEdit() {
  41. return this.examStudent.id;
  42. },
  43. },
  44. data() {
  45. return {
  46. visible: false,
  47. form: {
  48. file: "",
  49. fileName: "",
  50. },
  51. rules: {},
  52. loading: false,
  53. };
  54. },
  55. methods: {
  56. openDialog() {
  57. this.visible = true;
  58. },
  59. closeDialog() {
  60. this.visible = false;
  61. },
  62. selectFile(e) {
  63. this.form.file = e.target.files[0];
  64. this.form.fileName = this.form.file?.name;
  65. },
  66. async submitForm() {
  67. async function blobToArray(blob) {
  68. return new Promise((resolve) => {
  69. var reader = new FileReader();
  70. reader.addEventListener("loadend", function () {
  71. // reader.result contains the contents of blob as a typed array
  72. resolve(reader.result);
  73. });
  74. reader.readAsArrayBuffer(blob);
  75. });
  76. }
  77. const ab = await blobToArray(this.form.file);
  78. const md5 = MD5(ab);
  79. try {
  80. this.loading = true;
  81. await importExamStudent({
  82. examId: this.examId,
  83. file: this.form.file,
  84. fileName: this.form.fileName,
  85. md5,
  86. });
  87. this.$emit("reload");
  88. this.$notify({ title: "导入任务已成功启动", type: "success" });
  89. this.closeDialog();
  90. } finally {
  91. this.loading = false;
  92. }
  93. },
  94. },
  95. };
  96. </script>
  97. <style></style>