dioxus/examples/file_upload.rs

38 lines
1,016 B
Rust
Raw Normal View History

2023-04-25 13:16:11 +00:00
#![allow(non_snake_case)]
use dioxus::prelude::*;
fn main() {
dioxus_desktop::launch(App);
}
fn App(cx: Scope) -> Element {
let files_uploaded: &UseRef<Vec<String>> = use_ref(cx, Vec::new);
cx.render(rsx! {
input {
r#type: "file",
accept: ".txt, .rs",
2023-04-25 14:26:56 +00:00
multiple: true,
2023-04-25 13:16:11 +00:00
onchange: |evt| {
to_owned![files_uploaded];
async move {
if let Some(file_engine) = &evt.files {
let files = file_engine.files();
for file_name in &files {
2023-04-25 20:07:22 +00:00
if let Some(file) = file_engine.read_file_to_string(file_name).await{
2023-04-25 13:16:11 +00:00
files_uploaded.write().push(file);
}
}
}
}
},
}
ul {
for file in files_uploaded.read().iter() {
li { "{file}" }
}
}
})
}