© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•17mo ago•
2 replies
Krzysztof

Custom FileUpload

Is it possible to "fake" the underlying file for FileUpload field? I am connecting with external API and get the file in base64 format.

I am saving the file to the temporary disk so I think I should be able to provide the path to the file no problem.

I've found getUploadedFileUsing method on FileUpload but it does not seem to fire. Has anyone tried something similar?
Solution
If anyone is interested, what I end up doing is a custom component with setUp method overriden like that:

<?php

class CustomFileUpload extends FileUpload
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->afterStateHydrated(function (BaseFileUpload $component, string|array|null $state): void {
            $path = ...
            $component->state([((string) Str::uuid()) => $path]);
        });

        $this->deleteUploadedFileUsing(function ($file) {
            // delete here
        });

        $this->afterStateUpdated(function (BaseFileUpload $component, $state) {
            if ($state instanceof TemporaryUploadedFile) {
                // upload via api
            }

            if (blank($state)) {
                return;
            }

            if (is_array($state)) {
                return;
            }

            $component->state([(string) Str::uuid() => $state]);
        });
        $this->getUploadedFileUsing(static function (BaseFileUpload $component, string $file, string|array|null $storedFileNames): ?array {
            $url = ...
            $file = ...

            return [
                'name' => basename($file),
                'size' => $shouldFetchFileInformation ? $storage->size($file) : 0,
                'type' => $shouldFetchFileInformation ? $storage->mimeType($file) : null,
                'url' => $url,
            ];
        });
    }
}
<?php

class CustomFileUpload extends FileUpload
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->afterStateHydrated(function (BaseFileUpload $component, string|array|null $state): void {
            $path = ...
            $component->state([((string) Str::uuid()) => $path]);
        });

        $this->deleteUploadedFileUsing(function ($file) {
            // delete here
        });

        $this->afterStateUpdated(function (BaseFileUpload $component, $state) {
            if ($state instanceof TemporaryUploadedFile) {
                // upload via api
            }

            if (blank($state)) {
                return;
            }

            if (is_array($state)) {
                return;
            }

            $component->state([(string) Str::uuid() => $state]);
        });
        $this->getUploadedFileUsing(static function (BaseFileUpload $component, string $file, string|array|null $storedFileNames): ?array {
            $url = ...
            $file = ...

            return [
                'name' => basename($file),
                'size' => $shouldFetchFileInformation ? $storage->size($file) : 0,
                'type' => $shouldFetchFileInformation ? $storage->mimeType($file) : null,
                'url' => $url,
            ];
        });
    }
}
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Custom FileUpload action
FilamentFFilament / ❓┊help
17mo ago
FileUpload custom preview.
FilamentFFilament / ❓┊help
2y ago
Custom preview for FileUpload
FilamentFFilament / ❓┊help
2y ago
Fileupload (Filepond) custom preview
FilamentFFilament / ❓┊help
3y ago