Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/official-site/content/components/csv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
icon: download
category: http-responses
summary: Export query rows as a downloadable CSV file.
top:
- name: bom
type: BOOLEAN
description: Whether to include a Byte Order Mark (a special character indicating the character encoding)
at the beginning of the file. This is useful for Excel compatibility.
- class
- name: color
type: COLOR
description: Color of the button. Ignored when used as a header component.
- name: filename
type: TEXT
description: The name of the file that should be downloaded (without the extension).
- name: icon
type: ICON
description: Name of the icon (from tabler-icons.io) to display in the button. Ignored when used as a header
component.
- id
- name: separator
type: TEXT
description: How individual values should be separated in the CSV. "," by default, set it to "\t" for tab-separated
values.
- name: size
type: TEXT
description: The size of the button (e.g., sm, lg). Ignored when used as a header component.
- name: title
type: TEXT
required: true
description: The text displayed on the download button.
---

Lets the user download data as a CSV file.
Each column from the items in the component will map to a column in the resulting CSV.

When `csv` is used as a **header component** (without a [shell](?component=shell)), it will trigger a download of the CSV file directly on page load.
If the csv file to download is large, we recommend using this approach.

When used inside a page (after calling the shell component), this will add a button to the page that lets the user download the CSV file.
The button will need to load the entire contents of the CSV file in memory, inside the browser, even if the user does not click on it.
If the csv file to download is large, we recommend using this component without a shell component in order to efficiently stream the data to the browser.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
rows:
- component: button
- title: Download my data
link: /examples/csv_download.sql
---

### Header component: creating a CSV download URL

You can create a page that will trigger a download of the CSV file when the user visits it.
The contents will be streamed efficiently from the database to the browser, without being fully loaded in memory.
This makes it possible to download even very large files without overloading the database server, the web server, or the client's browser.

#### `csv_download.sql`

```sql
select 'csv' as component, 'example.csv' as filename;
SELECT * FROM my_large_table;
```

Link to `csv_download.sql` from a separate page with the button below. Opening the link starts the download without embedding the CSV data in the page that contains the button.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
rows:
- component: csv
title: Download my data
filename: people
icon: file-download
color: green
separator: ;
bom: true
- Forename: Ophir
Surname: Lojkine
Pseudonym: lovasoa
- Forename: Linus
Surname: Torvalds
Pseudonym: torvalds
---

### CSV download button

This will generate a button to download the CSV file.
The button element itself will embed the entire contents of the CSV file, so it should not be used for large files.
The file will be entirely loaded in memory on the user's browser, even if the user does not click on the button.
For smaller files, this is easier and faster to use than creating a separate SQL file to generate the CSV.
48 changes: 48 additions & 0 deletions examples/official-site/content/components/download.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
icon: download
since: 0.37.0
category: http-responses
summary: Send a file directly to the visitor.
top:
- name: data_url
type: TEXT
required: true
description: '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.'
- name: filename
type: TEXT
description: '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.'
---

The *download* component lets a page immediately return a file to the visitor.

Instead of showing a web page, it sends the file's bytes as the whole response,
so it should be used **at the very top of your SQL page** (before the shell or any other page contents).
It is an error to use this component after another component that would display content.

## Sending a file

- You provide the file content using a [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
A data URL is just a text string that contains both the file type and the actual data.
- Optionally, you provide a "filename" so the browser shows a proper Save As name.
If you do not provide a filename, many browsers will try to display the file inline (for example images or JSON), depending on the content type.
- You link to the page that uses the download component from another page, using the [button](/component.sql?component=button) component for example.

## Data URL format

- It looks like this: `data:[content-type][;base64],DATA`
- Examples:
- Plain text (URL-encoded): `data:text/plain,Hello%20world`
- JSON (URL-encoded): `data:application/json,%7B%22message%22%3A%22Hi%22%7D`
- Binary data (Base64): `data:application/octet-stream;base64,SGVsbG8h`

Tips:
- Use URL encoding when you have textual data. You can use [`sqlpage.url_encode(source_text)`](/functions?function=url_encode) to encode the data.
- Use Base64 when you have binary data (images, PDFs, or content that may include special characters).
- 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.

> Keep in mind that large files are better served from disk or object storage. Data URLs are best for small to medium files.
There is a big performance penalty for loading large files as data URLs, so it is not recommended.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Simple plain text file
Download a small text file. The content is URL-encoded (spaces become %20).

```sql
select
'download' as component,
'data:text/plain,Hello%20SQLPage%20world!' as data_url,
'hello.txt' as filename;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Download a PDF file from the server

Download a PDF file with the proper content type so PDF readers recognize it.
Uses [`sqlpage.read_file_as_data_url(file_path)`](/functions?function=read_file_as_data_url) to read the file from the server.

```sql
select
'download' as component,
'report.pdf' as filename,
sqlpage.read_file_as_data_url('report.pdf') as data_url;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Serve an image stored as a BLOB in the database

### Automatically detect the mime type

If you have a table with a column `content` that contains a BLOB
(depending on the database, the type may be named `BYTEA`, `BLOB`, `VARBINARY`, or `IMAGE`),
you can just return its contents directly, and SQLPage will automatically detect the mime type,
and convert it to a data URL.

```sql
select
'download' as component,
content as data_url
from document
where id = $doc_id;
```

### Customize the mime type

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,
and manually create your own data URL.

```sql
select
'download' as component,
'data:' || doc.mime_type || ';base64,' || encode(doc.content::bytea, 'base64') as data_url
from document as doc
where doc.id = $doc_id;
```

- 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.
- In MySQL and MariaDB, you can use the [TO_BASE64(str)](https://mariadb.com/docs/server/reference/sql-functions/string-functions/to_base64) function.
55 changes: 0 additions & 55 deletions examples/official-site/sqlpage/migrations/01_documentation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,58 +54,3 @@ SELECT example.rowid AS example_id,
FROM fields
) || ';', char(10) || char(10)) FROM json_each(example.properties) AS statement) AS source
FROM example;



INSERT INTO component(name, icon, description) VALUES
('csv', 'download', 'Lets the user download data as a CSV file.
Each column from the items in the component will map to a column in the resulting CSV.

When `csv` is used as a **header component** (without a [shell](?component=shell)), it will trigger a download of the CSV file directly on page load.
If the csv file to download is large, we recommend using this approach.

When used inside a page (after calling the shell component), this will add a button to the page that lets the user download the CSV file.
The button will need to load the entire contents of the CSV file in memory, inside the browser, even if the user does not click on it.
If the csv file to download is large, we recommend using this component without a shell component in order to efficiently stream the data to the browser.
');

INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'csv', * FROM (VALUES
-- top level
('separator', 'How individual values should be separated in the CSV. "," by default, set it to "\t" for tab-separated values.', 'TEXT', TRUE, TRUE),
('title', 'The text displayed on the download button.', 'TEXT', TRUE, FALSE),
('filename', 'The name of the file that should be downloaded (without the extension).', 'TEXT', TRUE, TRUE),
('icon', 'Name of the icon (from tabler-icons.io) to display in the button. Ignored when used as a header component.', 'ICON', TRUE, TRUE),
('color', 'Color of the button. Ignored when used as a header component.', 'COLOR', TRUE, TRUE),
('size', 'The size of the button (e.g., sm, lg). Ignored when used as a header component.', 'TEXT', TRUE, TRUE),
('bom', 'Whether to include a Byte Order Mark (a special character indicating the character encoding) at the beginning of the file. This is useful for Excel compatibility.', 'BOOLEAN', TRUE, TRUE)
) x;

INSERT INTO example(component, description, properties) VALUES
('csv', '
### Header component: creating a CSV download URL

You can create a page that will trigger a download of the CSV file when the user visits it.
The contents will be streamed efficiently from the database to the browser, without being fully loaded in memory.
This makes it possible to download even very large files without overloading the database server, the web server, or the client''s browser.

#### `csv_download.sql`

```sql
select ''csv'' as component, ''example.csv'' as filename;
SELECT * FROM my_large_table;
```

Link to `csv_download.sql` from a separate page with the button below. Opening the link starts the download without embedding the CSV data in the page that contains the button.
',
json('[{"component":"button"}, {"title": "Download my data", "link": "/examples/csv_download.sql"}]')),
('csv', '
### CSV download button

This will generate a button to download the CSV file.
The button element itself will embed the entire contents of the CSV file, so it should not be used for large files.
The file will be entirely loaded in memory on the user''s browser, even if the user does not click on the button.
For smaller files, this is easier and faster to use than creating a separate SQL file to generate the CSV.
',
json('[{"component":"csv", "title": "Download my data", "filename": "people", "icon": "file-download", "color": "green", "separator": ";", "bom": true}, '||
'{"Forename": "Ophir", "Surname": "Lojkine", "Pseudonym": "lovasoa"},' ||
'{"Forename": "Linus", "Surname": "Torvalds", "Pseudonym": "torvalds"}]'));
140 changes: 0 additions & 140 deletions examples/official-site/sqlpage/migrations/65_download.sql

This file was deleted.

Loading
Loading