فهرست منبع

cugr: 图片校验,axios 上传pdf

Michael Wang 5 سال پیش
والد
کامیت
05a76ebf31
2فایلهای تغییر یافته به همراه144 افزوده شده و 3 حذف شده
  1. 92 3
      src/features/OfflineExam/OfflineExamUploadCug.vue
  2. 52 0
      src/features/OfflineExam/imageToPdf.js

+ 92 - 3
src/features/OfflineExam/OfflineExamUploadCug.vue

@@ -36,6 +36,8 @@
 </template>
 
 <script>
+import { printCurrentPage } from "./imageToPdf";
+
 export default {
   name: "EcsOfflineUploadCug",
   props: {
@@ -100,6 +102,7 @@ export default {
             return "Unknown filetype";
         }
       }
+      console.log(file);
 
       const filereader = new FileReader();
       let uploads = [];
@@ -146,7 +149,28 @@ export default {
             }
           } else if (["image/jpeg"].includes(getMimetype(hex))) {
             if (file.name.endsWith(".jpeg") || file.name.endsWith(".jpg")) {
-              resolve();
+              printCurrentPage()
+                .then(filename => {
+                  // const fs = nodeRequire("fs");
+                  const path = window.nodeRequire("path");
+                  const fileNew = {
+                    name: path.basename(filename),
+                    path: filename,
+                    type: "application/pdf",
+                    // lastModified
+                    // lastModifiedDate
+                    // size
+                  };
+                  this.file = fileNew;
+                  file = fileNew;
+                  // console.log(this.$refs.uploadComp.fileList);
+                  // this.$refs.uploadComp.fileList = [file];
+                  // console.log(this.$refs.uploadComp.fileList);
+                  resolve();
+                })
+                .catch(() => {
+                  reject();
+                });
             } else {
               this.loadingStatus = false;
               // this.$refs.uploadComp.fileList.splice(0);
@@ -224,7 +248,7 @@ export default {
         desc: file.name + " 太大,作答文件不能超过30M.",
       });
     },
-    handleBeforeUpload(file) {
+    async handleBeforeUpload(file) {
       const suffix = file.name.split(".").pop();
       if (suffix.toLowerCase() !== suffix) {
         this.$Notice.error({
@@ -233,6 +257,66 @@ export default {
         });
         return Promise.reject("file suffix should be lower case");
       }
+      if (file.name.endsWith(".jpeg") || file.name.endsWith(".jpg")) {
+        new Promise((resolve, reject) => {
+          if (this.course.offlineFileUrl) {
+            this.$Modal.confirm({
+              title: "已有作答附件,是否覆盖?",
+              onCancel: () => reject(-1),
+              onOk: () => resolve(),
+            });
+          } else {
+            resolve();
+          }
+        }).then(() => {
+          return printCurrentPage()
+            .then(filename => {
+              const fs = window.nodeRequire("fs");
+              const path = window.nodeRequire("path");
+              // const fileNew = {
+              //   name: path.basename(filename),
+              //   // filename: path.basename(filename),
+              //   uri: "file://" + filename,
+              //   // path: filename,
+              //   type: "application/pdf",
+              // };
+              const fileNew = new File(
+                [fs.readFileSync(filename)],
+                path.basename(filename),
+                { type: "application/pdf" } // what I upload is image.
+              );
+              this.file = fileNew;
+
+              var stats = fs.statSync(filename);
+              var fileSizeInBytes = stats["size"];
+              if (fileSizeInBytes > 1024 * 30 * 1024) {
+                this.handleMaxSize();
+                throw "exceed max size";
+              }
+
+              const formData = new FormData();
+              formData.append("fileType", "pdf");
+              formData.append("file", fileNew);
+
+              return this.$http
+                .post(
+                  "/api/ecs_oe_admin/offlineExam/submitPaper?examRecordDataId=" +
+                    this.course.examRecordDataId,
+                  formData
+                )
+                .then(() => {
+                  this.handleSuccess();
+                })
+                .catch(() => {
+                  this.handleError();
+                });
+            })
+            .catch(error => {
+              console.log(error);
+            });
+        });
+        return Promise.reject("图片另外路径上传");
+      }
 
       return new Promise((resolve, reject) => {
         if (this.course.offlineFileUrl) {
@@ -245,7 +329,12 @@ export default {
           resolve();
         }
       }).then(() => {
-        if (file.type.includes("/pdf")) {
+        if (
+          file.type.includes("/pdf")
+          //  ||
+          // file.type.includes("/jpeg") ||
+          // file.type.includes("/jpg")
+        ) {
           this.fileType = "pdf";
         } else if (file.type.includes("/zip")) {
           this.fileType = "zip";

+ 52 - 0
src/features/OfflineExam/imageToPdf.js

@@ -0,0 +1,52 @@
+export function printCurrentPage() {
+  return new Promise((resolve, reject) => {
+    let remote = window.nodeRequire("electron").remote;
+
+    remote.getCurrentWindow().webContents.printToPDF(
+      {
+        marginsType: 1,
+        pageSize: "A4",
+        printBackground: true,
+        landscape: false,
+      },
+      (error, data) => {
+        if (error) {
+          // console.log(error);
+          this.$alert(error, "失败", {
+            confirmButtonText: "确定",
+          });
+          this.isPrint = false;
+          // throw error;
+          reject(error);
+        }
+
+        // this.isPrint = false;
+
+        //Synchronous
+        // let filename = dialog.showSaveDialog(WIN, options);
+        // // console.log(filename);
+        // if (!filename) return;
+        const tmpFolder = window.nodeRequire("os").tmpdir();
+        const filename = tmpFolder + "/提交答案-" + Date.now() + ".pdf";
+
+        const fs = window.nodeRequire("fs");
+        // TODO: choose pdf file name
+        // 按数据模型文档,此处没有file name,所以暂时只能是固定的名称
+        fs.writeFile(filename, data, error => {
+          if (error) {
+            // this.$alert(error, "失败", {
+            //   confirmButtonText: "确定",
+            // });
+            // throw error;
+            reject(error);
+          }
+          console.log("Write PDF successfully." + filename);
+          resolve(filename);
+          // this.$alert("导出PDF成功", "成功", {
+          //   confirmButtonText: "确定",
+          // });
+        });
+      }
+    );
+  });
+}