Downloading Files
How to implement file downloading in Hilla React applications.
Hilla endpoints only respond to POST requests and don’t support file downloads. Since Hilla applications use Spring, though, you can leverage Spring’s capabilities to implement file downloading.
Select the server mapping that best suits your application. Then create an ad-hoc endpoint to handle file downloads.
Next, configure security using the usual Spring Security configuration semantics.
In the following example, the view downloads a file and passes an ID as a parameter. The endpoint generates the file and sends it to the client. The security is configured so that only authenticated users can download files.
const id = useSignal('');
return (
<div className="flex p-m gap-m items-end">
<TextField
label="ID"
value={id.value}
onValueChanged={(e) => {
id.value = e.detail.value;
}}
/>
{id.value && (
<a href={`/download/${id.value}`} download>
Download
</a>
)}
</div>
);