Skip to content

Commit b2625dd

Browse files
author
romain
committed
Add very simple download files doc
1 parent 5adf780 commit b2625dd

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

doc/index.rst

+45
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,51 @@ The files will be available in a regular ``$request->files`` files bag::
13261326
need to specify ``multiple`` attribute on HTML element and end ``name``
13271327
with ``[]``.
13281328

1329+
.. _downloads:
1330+
1331+
Downloading files
1332+
-----------------------
1333+
1334+
Currently, Live Components do not natively support returning file responses directly from a LiveAction. However, you can implement file downloads by redirecting to a route that handles the file response.
1335+
1336+
Create a LiveAction that generates the URL for the file download and returns a `RedirectResponse`::
1337+
1338+
use Symfony\Component\HttpFoundation\RedirectResponse;
1339+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1340+
use Symfony\UX\LiveComponent\Attribute\LiveAction;
1341+
1342+
// ...
1343+
1344+
class MyDownloadButton
1345+
{
1346+
// ...
1347+
1348+
#[LiveAction]
1349+
public function initiateDownload(UrlGeneratorInterface $urlGenerator): RedirectResponse
1350+
{
1351+
$url = $urlGenerator->generate('app_file_download');
1352+
return new RedirectResponse($url);
1353+
}
1354+
}
1355+
1356+
.. code-block:: html+twig
1357+
1358+
<div {{ attributes }} data-turbo="false">
1359+
<button
1360+
data-action="live#action"
1361+
data-live-action-param="initiateDownload"
1362+
>
1363+
Download
1364+
</button>
1365+
</div>
1366+
1367+
When Turbo is enabled, it will intercept the redirect and initiate a second request for the download URL. Adding `data-turbo="false"` ensures that the download URL is called only once.
1368+
1369+
.. note::
1370+
1371+
Native support for file downloads in Live Components is under development. For more details, refer to the related `pull request #2483 <https://github.com/symfony/ux/pull/2483>`_.
1372+
1373+
13291374
.. _forms:
13301375

13311376
Forms

0 commit comments

Comments
 (0)