-
there are only buttons and text in examples |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Yes. The answer to the question "How?" depends a lot on how much you already know about the various Web APIs involved, how comfortable you are accessing web APIs from Rust using the Here's an example another user created that I found by searching in our Discord -- I don't take any credit for it. #[component]
fn UploadFileForm<F>(on_upload_finished: F) -> impl IntoView
where
F: Fn(bool) + 'static + Clone,
{
let error: RwSignal<Option<Box<dyn Error>>> = create_rw_signal(None);
let form_ref: NodeRef<html::Form> = create_node_ref();
let on_submit = move |ev: SubmitEvent| {
ev.prevent_default();
let form = form_ref.get().expect("noform");
let action = form
.get_attribute("action")
.unwrap_or_default()
.to_lowercase();
let form_data = web_sys::FormData::new_with_form(&form).unwrap_throw();
spawn_local(async move {
let res = gloo_net::http::Request::post(&action)
.header("Accept", "application/json")
.body(form_data)
.unwrap()
.send()
.await;
if let Err(err) = res {
log::error!("<Form/> error while POSTing: {err:#?}");
error.try_set(Some(Box::new(err)));
}
});
let has_error = error.with_untracked(|value| value.is_some());
on_upload_finished(has_error);
};
view! {
<Form error=error node_ref=form_ref enctype="multipart/form-data".to_string() action="/upload" on:submit=on_submit>
<label>"Upload file" <input type="file" id="path" name="path"/></label>
<input type="submit" value="Upload"/>
</Form>
}
} Alternately, with the new server functions on the main branch we have examples of file upload using multipart form data leptos/examples/server_fns_axum/src/app.rs Lines 321 to 384 in 7d1ce45 |
Beta Was this translation helpful? Give feedback.
Yes.
The answer to the question "How?" depends a lot on how much you already know about the various Web APIs involved, how comfortable you are accessing web APIs from Rust using the
web-sys
crate, etc.Here's an example another user created that I found by searching in our Discord -- I don't take any credit for it.