symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
787 stars 278 forks source link

[LiveComponent] Live Actions cannot handle file downloads #1516

Open richardhj opened 4 months ago

richardhj commented 4 months ago

As live actions are treated as regular controller actions, they should also support file responses.


#[LiveAction]
public function submit(): BinaryFileResponse
{
    $this->validate();

    return $this->file($this->exporter->generate($this->items));
}

Currently, file downloads are not downloaded but parsed..?

Screenshot 2024-02-18 at 13 28 23

Edit (current workaround)

public function submit(UriSigner $uriSigner): Response
{
    $this->validate();

    $file = $this->exporter->generate($this->items)

    $url = $this->generateUrl('admin_download', ['file' => base64_encode($file)]);

    return $this->redirect($uriSigner->sign($url));
}
smnandre commented 4 months ago

Hello @richardhj

there is no support of File responses currently in LiveComponent

Couple of solutions:

--

Some quotes cherry-picked from the RFC9110, in favor of the POST -> redirection -> GET solution

If one or more resources has been created on the origin server as a result of successfully processing a POST request, the origin server SHOULD send a 201 (Created) response containing a Location header field that provides an identifier for the primary resource created (Section 10.2.2) and a representation that describes the status of the request while referring to the new resource(s).

If the result of processing a POST would be equivalent to a representation of an existing resource, an origin server MAY redirect the user agent to that resource by sending a 303 (See Other) response with the existing resource's identifier in the Location field. This has the benefits of providing the user agent a resource identifier and transferring the representation via a method more amenable to shared caching

--

Now what seems weird to me is how the response seems handled and displayed in your example/capture... that may need some improvement / bug fixes :)

richardhj commented 4 months ago

Hi @smnandre, thats valuale information, I implemented a redirect to a "download controller" 👍 👍

However, that redirect must be uri-signed, so I can make sure that only "authorized" files can be downloaded. Maybe there is a way, Live component may support content-disposition in the future? 😄

norkunas commented 3 months ago

same need here, so maybe this should be considered to be in core :thinking:

barbieswimcrew commented 1 week ago

would be awesome if file response handling would work one day ❤️

stefpe commented 1 week ago

+1

x-vlad-x commented 1 week ago

+1

sebbemunich commented 1 week ago

+1

richardhj commented 1 week ago

Hey guys, this is not how OSS work and you are spamming other people's mailboxes.

smnandre commented 1 week ago

Hi @stefpe, @x-vlad-x, @sebbemunich,

I understand your desire to see this feature implemented. However, as @richardhj mentioned, this is an open-source project, so you can either:

If you want to support @barbieswimcrew's suggestion, thumbing up the first message in the issue is the best way, as it allows us to sort by this.


I performed a brief test with this method, which could serve as an implementation idea. This is similar to how Livewire implements this feature.

Here are some considerations for the implementation:

However, implementing this will take some time, which I currently do not have, as we all work on this in our free time.

(and I still believe that redirecting to a file is a much cleaner solution 😄 )