Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix: supporting multiple file input elements #401

Merged
merged 2 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/multiuploads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from justpy import *

def handle_submit(c, msg):
fl=msg.page.file_list
fl.components.clear()
for fd in msg.form_data:
if "files" in fd:
for f in fd["files"]:
Li(text=f"File uploaded: {f['name']} of {len(f['file_content'])}", a=fl)

def p1():
wp=WebPage()
f=Form(submit=handle_submit, a=wp)
Input(name="f1", type="file", a=f)
Input(name="f2", type="file", a=f)
Input(type="submit", value="OK", a=f)
wp.file_list=Ol(a=wp)
return wp


WebPage.tailwind=False
justpy(p1, start_server=(__name__=="__main__"))
40 changes: 20 additions & 20 deletions justpy/templates/js/html_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ Vue.component('html_component', {
event.stopPropagation();
var form_elements_list = [];
var form_elements = form_reference.elements;
var reader = new FileReader();
var file_readers = [];
var reader_ready = [];
var file_content = [];
var file_element_position = null;
var uploaders=[];

for (var i = 0; i < form_elements.length; i++) {
var attributes = form_elements[i].attributes;
Expand All @@ -103,15 +99,15 @@ Vue.component('html_component', {
attr_dict['id'] = form_elements[i].id;

if ((attr_dict['html_tag'] == 'input') && (input_type == 'file') && (files_chosen[attr_dict['id']])) {
file_element_position = i;
reader_ready = [];
let uploader={ file_readers: [], file_content:[], file_element_position: i, reader_ready: [] };
uploaders.push(uploader);
attr_dict['files'] = [];
const file_list = files_chosen[attr_dict['id']];
const num_files = file_list.length;
for (let j = 0; j < num_files; j++) {
reader_ready.push(false);
file_content.push('pending');
file_readers.push(new FileReader());
uploader.reader_ready.push(false);
uploader.file_content.push('pending');
uploader.file_readers.push(new FileReader());
attr_dict['files'].push({
file_content: 'pending',
name: file_list[j].name,
Expand All @@ -121,25 +117,29 @@ Vue.component('html_component', {
});
}
for (let j = 0; j < num_files; j++) {
file_readers[j].onload = function (e) {
file_content[j] = e.target.result.substring(e.target.result.indexOf(",") + 1);
reader_ready[j] = true;
uploader.file_readers[j].onload = function (e) {
console.log("loaded");
uploader.file_content[j] = e.target.result.substring(e.target.result.indexOf(",") + 1);
uploader.reader_ready[j] = true;
};
file_readers[j].readAsDataURL(file_list[j]);
uploader.file_readers[j].readAsDataURL(file_list[j]);
}
}

form_elements_list.push(attr_dict);
}

function check_readers() {
if (reader_ready.every(function (x) {
reader_ready_all=uploaders.flatMap((uploader)=> uploader.reader_ready);
if (reader_ready_all.every(function (x) {
return x
})) {
const file_element = form_elements_list[file_element_position];
for (uploader of uploaders) {
const file_element = form_elements_list[uploader.file_element_position];

for (let i = 0; i < file_element.files.length; i++) {
file_element.files[i].file_content = file_content[i];
for (let i = 0; i < file_element.files.length; i++) {
file_element.files[i].file_content = uploader.file_content[i];
}
}
eventHandler(props, event, form_elements_list);
return;
Expand All @@ -149,7 +149,7 @@ Vue.component('html_component', {
setTimeout(check_readers, 300);
}

if (file_element_position === null) {
if (uploaders.length==0) {
eventHandler(props, event, form_elements_list);
} else {
check_readers();
Expand Down Expand Up @@ -334,4 +334,4 @@ Vue.component('html_component', {
}
});

// {% endraw %}
// {% endraw %}