dioxus/examples/file_upload.rs

53 lines
1.5 KiB
Rust
Raw Normal View History

2023-04-25 13:16:11 +00:00
#![allow(non_snake_case)]
use dioxus::prelude::*;
2023-06-05 08:47:04 +00:00
use tokio::time::sleep;
2023-04-25 13:16:11 +00:00
fn main() {
dioxus_desktop::launch(App);
}
fn App(cx: Scope) -> Element {
let enable_directory_upload = use_state(cx, || false);
2023-04-25 13:16:11 +00:00
let files_uploaded: &UseRef<Vec<String>> = use_ref(cx, Vec::new);
cx.render(rsx! {
label {
input {
r#type: "checkbox",
checked: "{enable_directory_upload}",
oninput: move |evt| {
2023-09-01 20:38:55 +00:00
enable_directory_upload.set(evt.value().parse().unwrap());
},
},
"Enable directory upload"
}
2023-04-25 13:16:11 +00:00
input {
r#type: "file",
accept: ".txt,.rs",
2023-04-25 14:26:56 +00:00
multiple: true,
directory: **enable_directory_upload,
2023-04-25 13:16:11 +00:00
onchange: |evt| {
to_owned![files_uploaded];
async move {
2023-09-01 20:38:55 +00:00
if let Some(file_engine) = &evt.files() {
2023-04-25 13:16:11 +00:00
let files = file_engine.files();
2023-06-05 08:47:04 +00:00
for file_name in files {
sleep(std::time::Duration::from_secs(1)).await;
files_uploaded.write().push(file_name);
2023-04-25 13:16:11 +00:00
}
}
}
},
2023-06-05 08:47:04 +00:00
},
div { "progress: {files_uploaded.read().len()}" },
2023-04-25 13:16:11 +00:00
ul {
for file in files_uploaded.read().iter() {
li { "{file}" }
}
}
})
}