-
I am trying to do a multipart file upload with Leptos and I am a bit stuck at the moment. First, I tried to get the file content from the input element with the I enabled let elem = ev.unchecked_into::<HtmlInputElement>();
let files = elem.files(); But no matter which combination I try, When I do something like this, I can actually verify the correct file name being set but still with files == None: let on_change = move |ev: Event| {
let val = event_target_value(&ev);
let elem = ev.unchecked_into::<HtmlInputElement>();
let files = elem.files();
log!("value: {:?} files: {:?}", val, files);
}; I then tried to figure out how I could maybe do this directly from an Has anyone done this already and can maybe give me a hint? EDIT: With an let on_drop = move |ev: DragEvent| {
if let Some(data) = ev.data_transfer() {
if let Some(files) = data.files() {
for i in 0..files.length() {
let file = files.get(i).unwrap();
let name = file.name();
log!("file: {:?}", name);
}
}
}
}; |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Not sure what exactly's going wrong here but I'm able to get #[component]
pub fn App(cx: Scope) -> impl IntoView {
let file_input = create_node_ref::<html::Input>(cx);
view! { cx,
<form>
<input type="file" node_ref=file_input
on:change=move |ev| {
let files = file_input.get().unwrap().files();
log!("files: {:?}", files);
}
/>
<input type="submit"/>
</form>
}
} |
Beta Was this translation helpful? Give feedback.
-
Oh actually I do see what's going wrong: let elem = ev.unchecked_into::<HtmlInputElement>(); this is wrong. You're typecasting an let elem = ev.target().unwrap().unchecked_into::<HtmlInputElement>(); |
Beta Was this translation helpful? Give feedback.
-
Oh well, yeah, that did it. Such an easy fix. Actually, the hardest part when migrating from Javascript based frameworks to WASM is the conversion between the two. Things like "I know what this is in JS, but how can I convert it to Rust now". Thank you a lot! |
Beta Was this translation helpful? Give feedback.
Oh actually I do see what's going wrong:
this is wrong. You're typecasting an
Event
into anHtmlInputElement
. You want