Skip to main content

Upload

Struct Upload 

Source
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>

Source

pub const DEFAULT_MAX_BYTES: usize

The longest body an upload accepts unless Upload::max_bytes says otherwise: 8 MiB.

Source

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.

Source

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.

Source

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.

Source

pub fn path(&self) -> &'static str

The path the upload is served on, without its Base.

Source

pub fn limit(&self) -> usize

The longest body the upload accepts, in bytes.

Source

pub fn call_with( &self, files: FileInput<S>, args: impl IntoExpr<S, A>, ) -> Call<S>

A call that sends the files picked in files and args.

args is evaluated in the browser when the call runs, as for Action::call_with. A file input with no file picked sends no file.

Source§

impl<S> Upload<S, ()>

Source

pub fn call(&self, files: FileInput<S>) -> Call<S>

A call that sends the files picked in files, for an upload that takes no arguments.

Trait Implementations§

Source§

impl<S, A> Clone for Upload<S, A>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S, A> Copy for Upload<S, A>

Source§

impl<S, A> Debug for Upload<S, A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S, A> Freeze for Upload<S, A>

§

impl<S, A> RefUnwindSafe for Upload<S, A>

§

impl<S, A> Send for Upload<S, A>

§

impl<S, A> Sync for Upload<S, A>

§

impl<S, A> Unpin for Upload<S, A>

§

impl<S, A> UnsafeUnpin for Upload<S, A>

§

impl<S, A> UnwindSafe for Upload<S, A>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.