Преглед изворни кода

fix no file submit when upload large file

wangweimin пре 4 година
родитељ
комит
6fb2b6308c

+ 21 - 10
webiojs/src/handlers/input.ts

@@ -207,28 +207,39 @@ class FormController {
         element.on('submit', 'form', function (e) {
             e.preventDefault(); // avoid to execute the actual submit of the form.
 
+            element.find('button').prop("disabled", true);
+
             for (let name in that.name2input)
                 if (!that.name2input[name].check_valid())
                     return error_alert(t("error_in_input"));
 
-            let data: { [i: string]: any } = {};
+            let data_keys:string[] = [];
+            let data_values:any[] = [];
             $.each(that.name2input, (name, ctrl) => {
-                data[name] = ctrl.get_value();
+                data_keys.push(name as string);
+                data_values.push(ctrl.get_value());
             });
-            let on_process = undefined;
-            // 在有文件上传的表单中显示进度条
+
+            let on_process  = (loaded: number, total: number)=>{};
+            // show process bar when there is a file input field
             for (let item of that.spec.inputs) {
                 if (item.type == 'file') {
                     on_process = that.make_progress();
                     break;
                 }
             }
-            element.find('button').prop("disabled", true);
-            that.session.send_message({
-                event: "from_submit",
-                task_id: that.task_id,
-                data: data
-            }, on_process);
+            Promise.all(data_values).then((values) => {
+                let data: { [i: string]: any } = {};
+                for (let idx in data_keys){
+                    data[data_keys[idx]] = values[idx];
+                }
+                that.session.send_message({
+                    event: "from_submit",
+                    task_id: that.task_id,
+                    data: data
+                }, on_process);
+            });
+
         });
 
         this.element = element;

+ 2 - 2
webiojs/src/handlers/script.ts

@@ -25,12 +25,12 @@ export class ScriptHandler implements CommandHandler {
         }
 
         let res = null;
-        script = `return eval(${JSON.stringify(script)})`;
+        script = `return eval(${JSON.stringify(script)})`;  // lgtm [js/bad-code-sanitization]
         try {
             const script_func = new Function(...arg_names, script);
             res = script_func(...arg_vals);
         } catch (e) {
-            console.log('Exception occurred in user code of `run_script` command: \n%s', e)
+            console.log('Exception occurred in user code of `run_script` command: \n%s', e);
         }
         if (msg.spec.eval) {
             // credit: https://stackoverflow.com/questions/27746304/how-do-i-tell-if-an-object-is-a-promise

+ 1 - 0
webiojs/src/models/input/base.ts

@@ -23,6 +23,7 @@ export class InputItem {
         throw new Error("Not implement!");
     }
 
+    // return a value or promise
     get_value(): any {
         throw new Error("Not implement!");
     }

+ 17 - 13
webiojs/src/models/input/file.ts

@@ -18,7 +18,7 @@ const file_input_tpl = `
 export class File extends InputItem {
     static accept_input_types: string[] = ["file"];
 
-    data_url_value: { filename: string, dataurl: string, mime_type: string, last_modified: number, size: number }[] = []; // 待上传文件信息
+    file_content_promises: Promise<{ filename: string, dataurl: string, mime_type: string, last_modified: number, size: number }>[] = []; // 待上传文件信息
     valid = true;
 
     constructor(session: Session, task_id: string, spec: any) {
@@ -49,7 +49,7 @@ export class File extends InputItem {
         // 文件选中后先不通知后端
         let that = this;
         input_elem.on('change', function () {
-            that.data_url_value = [];
+            that.file_content_promises = [];
             let total_size = 0;
             that.valid = true;
             let file = (input_elem[0] as HTMLInputElement).files;
@@ -67,22 +67,26 @@ export class File extends InputItem {
                     that.valid = false;
                     that.update_input_helper(-1, {
                         'valid_status': false,
-                        'invalid_feedback': t("file_total_size_exceed",that._formate_size(that.spec.max_total_size))
+                        'invalid_feedback': t("file_total_size_exceed", that._formate_size(that.spec.max_total_size))
                     });
                     return;
                 }
                 if (!that.valid) return;
                 that.update_input_helper(-1, {'valid_status': 0});
 
-                fr.onload = function () {
-                    that.data_url_value.push({
-                        'filename': f.name,
-                        'size': f.size,
-                        'mime_type': f.type,
-                        'last_modified': f.lastModified / 1000,
-                        'dataurl': fr.result as string
-                    });
-                };
+                that.file_content_promises.push(new Promise((resolve, reject) => {
+                    fr.onload = function () {
+                        resolve({
+                            'filename': f.name,
+                            'size': f.size,
+                            'mime_type': f.type,
+                            'last_modified': f.lastModified / 1000,
+                            'dataurl': fr.result as string
+                        });
+                    };
+                }));
+
+
                 fr.readAsDataURL(f);
             }
 
@@ -110,7 +114,7 @@ export class File extends InputItem {
     }
 
     get_value(): any {
-        return this.data_url_value;
+        return Promise.all(this.file_content_promises);
     }
 
     after_add_to_dom(): any {