F
Filament2mo ago
Kyle

Just get the original filename?

Is there a way to get the uploaded file's original filename in afterStateUpdated of a FileUpload field? I want to keep the randomly-generated filename, but I want to get the original filename so I can $set() the text of some other fields based on the original filename of the uploaded file.
1 Reply
Kyle
KyleOP2mo ago
Found the solution myself.
FileUpload::make('path')
->live(onBlur: true)
->afterStateUpdated(function (Set $set, FileUpload $component) {
$state = $component->getRawState();
if ($first = reset($state)) {
$filename = $first->getClientOriginalName();
// Do whatever you want with the original filename
}
}),
FileUpload::make('path')
->live(onBlur: true)
->afterStateUpdated(function (Set $set, FileUpload $component) {
$state = $component->getRawState();
if ($first = reset($state)) {
$filename = $first->getClientOriginalName();
// Do whatever you want with the original filename
}
}),
You might want to make sure that the content of the state is a TemporaryUploadedFile and not a string. It does that sometimes.

Did you find this page helpful?