pub struct UploadedFile(/* private fields */);Expand description
A file uploaded with a multipart/form-data form. You get one as a field of a
validated struct, with the file’s validation rules on that field.
use laravel::http::UploadedFile;
use laravel::validation::{Valid, Validate};
#[derive(serde::Deserialize, Validate)]
#[validate(anyone)]
struct UpdateAvatar {
#[validate(required, max_kilobytes = 2048)]
avatar: UploadedFile,
}
async fn update_avatar(Valid(form): Valid<UpdateAvatar>) -> String {
let avatar = form.avatar;
let image = avatar.content_type().is_some_and(|kind| kind.starts_with("image/"));
let name = avatar.client_name();
format!("{name} ({} bytes, an image: {image})", avatar.size())
}Laravel writes the upload to a temporary file as it arrives, rather than keeping it in memory.
Clones share that file, and Laravel deletes it once the request and every clone are done with
it. To keep the upload, copy it to a storage disk with store.
Temporary files go in the system’s temporary directory, which you may change with the TMPDIR
environment variable. Some containers keep that directory in memory, so make sure it has room
for the largest uploads.
Implementations§
Source§impl UploadedFile
impl UploadedFile
Sourcepub fn store(&self, directory: impl Into<String>) -> PendingStore<'_>
pub fn store(&self, directory: impl Into<String>) -> PendingStore<'_>
Copy this upload into a directory of the default disk, under a random name. The name gets
an extension when the file’s contents are a recognized format.
To use another disk, call .disk("s3") before awaiting. Awaiting returns the file’s path
relative to the disk’s root, such as avatars/7kQ2...x9.png.
use laravel::http::UploadedFile;
async fn store_avatar(avatar: &UploadedFile) -> laravel::Result<String> {
avatar.store("avatars").disk("public").await
}§Errors
Awaiting fails when the disk isn’t configured, the path is invalid, the upload can’t be read, or the disk can’t be written.
§Panics
Awaiting panics outside an application context.
Sourcepub fn store_as(
&self,
directory: impl Into<String>,
name: impl Into<String>,
) -> PendingStore<'_>
pub fn store_as( &self, directory: impl Into<String>, name: impl Into<String>, ) -> PendingStore<'_>
Sourcepub fn client_name(&self) -> &str
pub fn client_name(&self) -> &str
The file’s name on the user’s computer. It’s the part of the name the browser sent after
the last / or \, so it’s never a path into another directory. The user still chooses the
name, and it may be .., so never use it as a file name without changing it.
Sourcepub fn client_extension(&self) -> Option<&str>
pub fn client_extension(&self) -> Option<&str>
The extension of the file’s name on the user’s computer, if it has one.
Sourcepub fn client_content_type(&self) -> Option<&str>
pub fn client_content_type(&self) -> Option<&str>
The content type the browser sent, which the user controls. For the type detected from the
file’s bytes, use content_type.
Sourcepub fn content_type(&self) -> Option<&str>
pub fn content_type(&self) -> Option<&str>
The file’s content type, detected from its bytes, such as image/png. Returns None
unless the bytes are in a recognized format, such as an image, audio, video, archive, or
document. Plain text isn’t a recognized format.
Sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
The extension for the file’s content, detected from its bytes, such as png, whatever name
the user gave the file. Returns None when content_type does.
Trait Implementations§
Source§impl Clone for UploadedFile
impl Clone for UploadedFile
Source§fn clone(&self) -> UploadedFile
fn clone(&self) -> UploadedFile
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for UploadedFile
impl Debug for UploadedFile
Source§impl<'de> Deserialize<'de> for UploadedFile
Only Request::validate and the extractors that call it can
decode a file, directly from an InputValue::File node.
impl<'de> Deserialize<'de> for UploadedFile
Only Request::validate and the extractors that call it can
decode a file, directly from an InputValue::File node.
Source§fn deserialize<D>(
deserializer: D,
) -> Result<UploadedFile, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<UploadedFile, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl From<&UploadedFile> for Attachment
Attach an uploaded file to an outgoing request. The client streams the file from its temporary
path and sends it with its original name. When the file’s bytes are in a recognized format, the
client also sends the content type detected from them.
impl From<&UploadedFile> for Attachment
Attach an uploaded file to an outgoing request. The client streams the file from its temporary path and sends it with its original name. When the file’s bytes are in a recognized format, the client also sends the content type detected from them.
use laravel::http::{UploadedFile, client};
async fn forward(photo: &UploadedFile) -> laravel::Result<&'static str> {
client::post("https://example.com/photos")
.attach("photo", photo)
.ensure_successful()
.await?;
Ok("stored")
}Source§fn from(file: &UploadedFile) -> Attachment
fn from(file: &UploadedFile) -> Attachment
Source§impl From<UploadedFile> for InputValue
impl From<UploadedFile> for InputValue
Source§fn from(file: UploadedFile) -> InputValue
fn from(file: UploadedFile) -> InputValue
Auto Trait Implementations§
impl Forget for UploadedFile
impl Freeze for UploadedFile
impl Move for UploadedFile
impl RefUnwindSafe for UploadedFile
impl Send for UploadedFile
impl Sync for UploadedFile
impl Unpin for UploadedFile
impl UnsafeUnpin for UploadedFile
impl UnwindSafe for UploadedFilewhere
Arc<Stored>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more