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...
Jump to solution
1 Reply
Solution
Krzysztof
Krzysztof2mo ago
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,
];
});
}
}
Want results from more Discord servers?
Add your server