-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'latest' into docs/horizontal-layout-slots
- Loading branch information
Showing
8 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: File Downloading | ||
page-title: How to implement file downloading in Hilla React applications | Vaadin | ||
description: How to implement file downloading in Hilla React applications. | ||
meta-description: Learn how to implement file downloading in Vaadin Hilla. | ||
order: 73 | ||
--- | ||
|
||
|
||
# Downloading Files | ||
|
||
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. | ||
|
||
[.example] | ||
-- | ||
[source,tsx] | ||
.`download.tsx` | ||
---- | ||
include::{root}/frontend/demo/fusion/forms/upload/download.tsx[tags=snippet,indent=0] | ||
---- | ||
[source,java] | ||
.`FileDownloadEndpoint.java` | ||
---- | ||
include::{root}/src/main/java/com/vaadin/demo/fusion/download/FileDownloadEndpoint.java[tags=snippet,indent=0] | ||
---- | ||
[source,java] | ||
.`SecurityConfig.java` | ||
---- | ||
include::{root}/src/main/java/com/vaadin/demo/SecurityConfig.java[tags=download,indent=0] | ||
---- | ||
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useSignal } from '@vaadin/hilla-react-signals'; | ||
import { TextField } from '@vaadin/react-components'; | ||
|
||
export default function DownloadView() { | ||
// tag::snippet[] | ||
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> | ||
); | ||
// end::snippet[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/vaadin/demo/fusion/download/FileDownloadEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.vaadin.demo.fusion.download; | ||
|
||
import org.springframework.core.io.ByteArrayResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class FileDownloadEndpoint { | ||
|
||
// tag::snippet[] | ||
@RequestMapping(value = "/download/{id}", method = RequestMethod.GET) | ||
public ResponseEntity<Resource> downloadFile(@PathVariable("id") String id) { | ||
String content = "File content for ID: " + id; | ||
Resource file = new ByteArrayResource(content.getBytes()); | ||
|
||
return ResponseEntity.ok() | ||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"file_" + id + ".txt\"") | ||
.body(file); | ||
} | ||
// end::snippet[] | ||
} |