Skip to main content

FormFile

Struct FormFile 

Source
pub struct FormFile { /* private fields */ }
Expand description

One file part of a multipart/form-data request.

Cloning a FormFile is cheap: all clones share the same underlying bytes or spooled temporary file, and the temporary file is removed when the last clone is dropped.

Implementations§

Source§

impl FormFile

Source

pub fn field_name(&self) -> &str

The form field name this file arrived under (e.g. "avatar").

Source

pub fn file_name(&self) -> &str

A filesystem-safe basename derived from the client-supplied filename.

Path separators, control characters and Windows-reserved characters are removed, so this is safe to pass to Path::join. Use FormFile::original_file_name if you need the raw value for display.

Source

pub fn original_file_name(&self) -> &str

The filename exactly as sent by the client, without sanitisation.

Never use this to build a filesystem path.

Source

pub fn content_type(&self) -> Option<&str>

The part’s declared content type, if the client sent one.

This is attacker-controlled metadata: treat it as a hint, not a guarantee.

Source

pub fn size(&self) -> u64

Size of the uploaded content in bytes.

Source

pub fn is_empty(&self) -> bool

Whether the uploaded content is empty.

Source

pub fn extension(&self) -> Option<String>

Lowercase file extension of FormFile::file_name, without the dot.

Source

pub fn path(&self) -> Option<&Path>

The spooled temporary file path, when the upload was large enough to be written to disk.

The path is only valid while this FormFile (or a clone) is alive. Prefer FormFile::save_as to hand a file to the rest of your application.

Source

pub async fn open(&self) -> Result<Box<dyn AsyncRead + Send + Unpin>, Error>

Open the uploaded content as an async reader.

Memory-backed files read from a reference-counted buffer without copying it; spooled files are opened from disk. Either way the read is streamed.

Source

pub async fn read_bytes(&self) -> Result<Vec<u8>, Error>

Read the whole upload into memory.

Prefer FormFile::open or FormFile::copy_to for files that may be large.

Source

pub async fn copy_to<W>(&self, writer: &mut W) -> Result<u64, Error>
where W: AsyncWrite + Unpin + ?Sized,

Stream the upload into writer, returning the number of bytes written.

Uses the framework’s 64 KiB copy buffer rather than the 8 KiB default, which matters when a multi-gigabyte upload is being persisted.

Source

pub async fn save_as(&self, destination: impl AsRef<Path>) -> Result<(), Error>

Stream the upload to destination, creating parent directories.

The bytes are written to a sibling temporary file first and renamed into place, so destination never contains a partially written upload.

Trait Implementations§

Source§

impl Clone for FormFile

Source§

fn clone(&self) -> FormFile

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 Debug for FormFile

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more