| 64 | * for file input, the `input_value` is in {multiple: bool, files: File[] } |
| 65 | * */ |
| 66 | submit(msg: ClientEvent, file_input_names: string[]) { |
| 67 | // See: deserialize_binary_event() in pywebio/platform/utils.py |
| 68 | let file_blobs:Blob[] = []; |
| 69 | for (let name of file_input_names) { |
| 70 | if (msg.data && msg.data[name]) { |
| 71 | // {multiple: bool, files: File[]} |
| 72 | let {multiple, files} = msg.data[name]; |
| 73 | msg.data[name] = multiple ? [] : null; // replace file value with initial value |
| 74 | file_blobs.push(...files.map((file: File) => serialize_file(file, name))); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (file_blobs.length) { |
| 79 | let toast = Toastify({ |
| 80 | text: `⏳${t("file_uploading")} 0%`, |
| 81 | duration: -1, |
| 82 | gravity: "top", |
| 83 | position: 'center', |
| 84 | backgroundColor: '#1565c0', |
| 85 | }); |
| 86 | toast.showToast(); |
| 87 | state.CurrentSession.send_buffer( |
| 88 | new Blob([serialize_json(msg), ...file_blobs], {type: 'application/octet-stream'}), |
| 89 | (loaded: number, total: number) => { |
| 90 | toast.toastElement.innerText = `⏳${t("file_uploading")} ${((loaded / total)*100).toFixed(2)}%`; |
| 91 | if (total - loaded < 100) toast.hideToast(); |
| 92 | } |
| 93 | ); |
| 94 | } else { |
| 95 | state.CurrentSession.send_message(msg); |
| 96 | } |
| 97 | } |
| 98 | } |