You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Introduce a new download component to facilitate file downloads.
- Implement download handling in the header context, supporting data URLs.
- Add a test for the download functionality to ensure correct behavior.
see #996
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@
10
10
- This allows you to trigger modals from any other component, including tables, maps, forms, lists and more.
11
11
- Since modals have their own url inside the page, you can now link to a modal from another page, and if you refresh a page while the modal is open, the modal will stay open.
12
12
- modals now have an `open` parameter to open the modal automatically when the page is loaded.
13
+
- New [download](https://sql-page.com/component.sql?component=download) component to let the user download files. The files may be stored as BLOBs in the database, local files on the server, or may be fetched from a different server.
13
14
14
15
## v0.36.1
15
16
- Fix regression introduced in v0.36.0: PostgreSQL money values showed as 0.0
- Binary data (Base64): `data:application/octet-stream;base64,SGVsbG8h`
27
+
28
+
Tips:
29
+
- Use URL encoding when you have textual data. You can use [`sqlpage.url_encode(source_text)`](/functions?function=url_encode) to encode the data.
30
+
- Use Base64 when you have binary data (images, PDFs, or content that may include special characters).
31
+
- Use [`sqlpage.read_file_as_data_url(file_path)`](/functions?function=read_file_as_data_url) to read a file from the server and return it as a data URL.
32
+
33
+
> Keep in mind that large files are better served from disk or object storage. Data URLs are best for small to medium files.
34
+
There is a big performance penalty for loading large files as data URLs, so it is not recommended.
35
+
',
36
+
'download',
37
+
'0.37.0'
38
+
);
39
+
40
+
-- Insert the parameters for the download component into the parameter table
41
+
INSERT INTO
42
+
parameter (
43
+
component,
44
+
name,
45
+
description,
46
+
type,
47
+
top_level,
48
+
optional
49
+
)
50
+
VALUES
51
+
(
52
+
'download',
53
+
'data_url',
54
+
'The file content to send, written as a data URL (for example: data:text/plain,Hello%20world or data:application/octet-stream;base64,SGVsbG8h). The part before the comma declares the content type and whether the data is base64-encoded. The part after the comma is the actual data.',
55
+
'TEXT',
56
+
TRUE,
57
+
FALSE
58
+
),
59
+
(
60
+
'download',
61
+
'filename',
62
+
'The suggested name of the file to save (for example: report.csv). When set, the browser will download the file as an attachment with this name. When omitted, many browsers may try to display the file inline depending on its content type.',
63
+
'TEXT',
64
+
TRUE,
65
+
TRUE
66
+
);
67
+
68
+
-- Insert usage examples of the download component into the example table
69
+
INSERT INTO
70
+
example (component, description)
71
+
VALUES
72
+
(
73
+
'download',
74
+
'
75
+
## Simple plain text file
76
+
Download a small text file. The content is URL-encoded (spaces become %20).
77
+
78
+
```sql
79
+
select
80
+
''download'' as component,
81
+
''data:text/plain,Hello%20SQLPage%20world!'' as data_url,
82
+
''hello.txt'' as filename;
83
+
```
84
+
'
85
+
),
86
+
(
87
+
'download',
88
+
'
89
+
## Download a PDF file from the server
90
+
91
+
Download a PDF file with the proper content type so PDF readers recognize it.
92
+
Uses [`sqlpage.read_file_as_data_url(file_path)`](/functions?function=read_file_as_data_url) to read the file from the server.
93
+
94
+
```sql
95
+
select
96
+
''download'' as component,
97
+
''report.pdf'' as filename,
98
+
sqlpage.read_file_as_data_url(''report.pdf'') as data_url;
99
+
```
100
+
'
101
+
),
102
+
(
103
+
'download',
104
+
'
105
+
## Serve an image stored as a BLOB in the database
106
+
107
+
In PostgreSQL, you can use the [encode(bytes, format)](https://www.postgresql.org/docs/current/functions-binarystring.html#FUNCTION-ENCODE) function to encode the file content as Base64.
108
+
109
+
```sql
110
+
select
111
+
''download'' as component,
112
+
''data:'' || doc.mime_type || '';base64,'' || encode(doc.content::bytea, ''base64'') as data_url
113
+
from document as doc
114
+
where doc.id = $doc_id;
115
+
```
116
+
117
+
- In Microsoft SQL Server, you can use the [BASE64_ENCODE(bytes)](https://learn.microsoft.com/en-us/sql/t-sql/functions/base64-encode-transact-sql) function to encode the file content as Base64.
118
+
- In MySQL and MariaDB, you can use the [TO_BASE64(str)](https://mariadb.com/docs/server/reference/sql-functions/string-functions/to_base64) function.
0 commit comments