pub struct Upload<S, A = ()> { /* private fields */ }Expand description
A server endpoint on the page whose signals are S, which receives
arguments A and the files picked in a FileInput.
Declare one as a const and use the same value on both sides, as with an
Action: the page calls it with Upload::call or
Upload::call_with, and the router registers its handler with
RouterExt::upload, which hands it an Uploaded<A>. The browser sends a
multipart/form-data body, which a form on another site can send too, so
the runtime adds a header such a form cannot set, and the router refuses a
request without it.
The body may be Upload::DEFAULT_MAX_BYTES long unless
Upload::max_bytes says otherwise. A longer one gets 413 Payload Too Large.
use hypersig::html::{button, div};
use hypersig::{FileInput, Signals, Upload};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Profile {
uploading: bool,
}
const PICTURE: FileInput<Profile> = FileInput::new("picture");
const AVATAR: Upload<Profile> = Upload::post("/avatar").max_bytes(1024 * 1024);
let _form = div()
.child(PICTURE.input().attr("accept", "image/*"))
.child(button().on_click(AVATAR.call(PICTURE).indicator(Profile::uploading())).text("Upload"));Implementations§
Source§impl<S, A> Upload<S, A>
impl<S, A> Upload<S, A>
Sourcepub const DEFAULT_MAX_BYTES: usize
pub const DEFAULT_MAX_BYTES: usize
The longest body an upload accepts unless Upload::max_bytes says otherwise: 8 MiB.
Sourcepub const fn post(path: &'static str) -> Self
pub const fn post(path: &'static str) -> Self
An upload served on POST path.
§Panics
Panics if path does not start with /. In a const, that is a
compile error.
Sourcepub const fn max_bytes(self, bytes: usize) -> Self
pub const fn max_bytes(self, bytes: usize) -> Self
The same upload, accepting a body of at most bytes bytes, the
arguments and the multipart framing included.
Sourcepub const fn under(self, base: Base) -> Self
pub const fn under(self, base: Base) -> Self
The same upload under base, which the page’s calls put before the path.
The router registers Upload::path without it. See Base.