F
Filamentβ€’2mo ago
Sujay Barma

How to get file size after file upload?

I am new to filament. I have requirement where after the file upload is complete. The 'Size' field in the form should have the value. Can someone explain how do I achieve this in Filament or what approach to use?
No description
Solution:
You'd have to use ->afterStateUpdated() on the file field then get the data and set it
Jump to solution
6 Replies
Solution
toeknee
toekneeβ€’2mo ago
You'd have to use ->afterStateUpdated() on the file field then get the data and set it
Sujay Barma
Sujay Barmaβ€’2mo ago
Thank you πŸ™
morty
mortyβ€’5w ago
Can you post your solution? I was just curious what it looked like. Nevermind, I see your form now in the first post. I was wondering whether to use a hidden input for this but it looks like you're using an actual input.
Gabs&Gabs
Gabs&Gabsβ€’5w ago
Once the file has been saved, can I enable a preview? Or just the name of the file
No description
No description
toeknee
toekneeβ€’4w ago
add ->preview() if it isn't previewing then check your console as where the file is you are probably getting a 404 if you haven't linked it or routed it correctly for access
Sujay Barma
Sujay Barmaβ€’4w ago
FileUpload::make('download_url')
->disk('public')
->directory('videos')
->label('Video File')
->afterStateUpdated(function ($state, callable $set) {
$fileSize = $state->getSize() / (1024 * 1024); // Size in KB
$set('size', number_format($fileSize, 2));
}),
TextInput::make('size')->readonly()
FileUpload::make('download_url')
->disk('public')
->directory('videos')
->label('Video File')
->afterStateUpdated(function ($state, callable $set) {
$fileSize = $state->getSize() / (1024 * 1024); // Size in KB
$set('size', number_format($fileSize, 2));
}),
TextInput::make('size')->readonly()
Code Snippet
Want results from more Discord servers?
Add your server