123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <template>
- <div>
- <Upload :headers="headers" :data="{fileType: fileType}" :before-upload="handleBeforeUpload" :action="'/api/ecs_oe_student/offlineExam/submitPaper?examRecordDataId='+course.examRecordDataId" :max-size="1024*30" :format="['pdf','zip']" accept="application/zip,application/pdf" :on-format-error="handleFormatError" :on-exceeded-size="handleMaxSize" :on-success="handleSuccess" :on-error="handleError" :show-upload-list="false">
- <Button icon="ios-cloud-upload-outline" class="qm-primary-button" style="width: 100%;">上传作答</Button>
- </Upload>
- <div v-if="file !== null">待上传文件: {{ file.name }}
- <Button :loading="loadingStatus">{{ loadingStatus ? '上传中...' : '上传' }}</Button>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "EcsOfflineUpload",
- data() {
- return {
- headers: {
- token: window.localStorage.getItem("token"),
- key: window.localStorage.getItem("key")
- },
- file: null,
- fileType: null,
- loadingStatus: false
- };
- },
- props: {
- course: Object
- },
- methods: {
- fileFormatCheck(file, resolve, reject) {
- function getMimetype(signature) {
- switch (signature) {
- case "89504E47":
- return "image/png";
- case "47494638":
- return "image/gif";
- case "25504446":
- return "application/pdf";
- case "FFD8FFDB":
- case "FFD8FFE0":
- case "FFD8FFE1":
- return "image/jpeg";
- case "504B0304":
- return "application/zip";
- case "504B34":
- return "application/zip";
- default:
- return "Unknown filetype";
- }
- }
- const filereader = new FileReader();
- let uploads = [];
- filereader.onloadend = evt => {
- if (evt.target.readyState === FileReader.DONE) {
- const uint = new Uint8Array(evt.target.result);
- let bytes = [];
- uint.forEach(byte => {
- bytes.push(byte.toString(16));
- });
- const hex = bytes.join("").toUpperCase();
- uploads.push({
- filename: file.name,
- filetype: file.type ? file.type : "Unknown/Extension missing",
- binaryFileType: getMimetype(hex),
- hex: hex
- });
- if (
- ["application/zip", "application/pdf"].includes(getMimetype(hex))
- ) {
- resolve();
- } else {
- console.log("binary file type check: not zip or pdf");
- this.$Notice.warning({
- title: "作答文件损坏",
- desc: file.name + " 文件无法以 .zip 或 .pdf 读取。"
- });
- this.file = null;
- this.loadingStatus = false;
- reject();
- }
- }
- };
- const blob = file.slice(0, 4);
- filereader.readAsArrayBuffer(blob);
- },
- handleSuccess() {
- this.file = null;
- this.loadingStatus = false;
- this.$Message.success("上传成功");
- this.$emit("reloadList");
- },
- handleError() {
- this.file = null;
- this.loadingStatus = false;
- this.$Message.error("上传失败");
- },
- handleFormatError(file) {
- this.$Notice.warning({
- title: "作答文件格式不对",
- desc: file.name + " 文件格式不对,请选择 .zip 或 .pdf 文件。"
- });
- },
- handleMaxSize(file) {
- this.$Notice.warning({
- title: "超出文件大小限制",
- desc: file.name + " 太大,作答文件不能超过30M."
- });
- },
- handleBeforeUpload(file) {
- if (file.type.includes("/pdf")) {
- this.fileType = "pdf";
- } else if (file.type.includes("/zip")) {
- this.fileType = "zip";
- }
- this.file = file;
- this.loadingStatus = true;
- return new Promise((resolve, reject) =>
- this.fileFormatCheck(file, resolve, reject)
- );
- }
- }
- };
- </script>
- <style lang="postcss">
- .list .ivu-upload-select {
- width: 100%;
- }
- </style>
|