From 9ce9b6ed13d144a52fbd9288f275d3ae23409e26 Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:34 +0000 Subject: [PATCH] Move CSV and file-download references to Markdown --- .../official-site/content/components/csv.md | 43 ++++++ .../csv/examples/01-download-url.md | 21 +++ .../csv/examples/02-download-button.md | 23 +++ .../content/components/download.md | 48 ++++++ .../download/examples/01-plain-text.md | 9 ++ .../components/download/examples/02-pdf.md | 11 ++ .../download/examples/03-image-blob.md | 32 ++++ .../sqlpage/migrations/01_documentation.sql | 55 ------- .../sqlpage/migrations/65_download.sql | 140 ------------------ .../migrations/82_reference_catalog.sql | 2 - .../99_shared_id_class_attributes.sql | 23 --- 11 files changed, 187 insertions(+), 220 deletions(-) create mode 100644 examples/official-site/content/components/csv.md create mode 100644 examples/official-site/content/components/csv/examples/01-download-url.md create mode 100644 examples/official-site/content/components/csv/examples/02-download-button.md create mode 100644 examples/official-site/content/components/download.md create mode 100644 examples/official-site/content/components/download/examples/01-plain-text.md create mode 100644 examples/official-site/content/components/download/examples/02-pdf.md create mode 100644 examples/official-site/content/components/download/examples/03-image-blob.md delete mode 100644 examples/official-site/sqlpage/migrations/65_download.sql delete mode 100644 examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql diff --git a/examples/official-site/content/components/csv.md b/examples/official-site/content/components/csv.md new file mode 100644 index 000000000..61481c179 --- /dev/null +++ b/examples/official-site/content/components/csv.md @@ -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. diff --git a/examples/official-site/content/components/csv/examples/01-download-url.md b/examples/official-site/content/components/csv/examples/01-download-url.md new file mode 100644 index 000000000..a7b4e3046 --- /dev/null +++ b/examples/official-site/content/components/csv/examples/01-download-url.md @@ -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. diff --git a/examples/official-site/content/components/csv/examples/02-download-button.md b/examples/official-site/content/components/csv/examples/02-download-button.md new file mode 100644 index 000000000..e0e771c6b --- /dev/null +++ b/examples/official-site/content/components/csv/examples/02-download-button.md @@ -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. diff --git a/examples/official-site/content/components/download.md b/examples/official-site/content/components/download.md new file mode 100644 index 000000000..10a36ef7a --- /dev/null +++ b/examples/official-site/content/components/download.md @@ -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. diff --git a/examples/official-site/content/components/download/examples/01-plain-text.md b/examples/official-site/content/components/download/examples/01-plain-text.md new file mode 100644 index 000000000..295457da5 --- /dev/null +++ b/examples/official-site/content/components/download/examples/01-plain-text.md @@ -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; +``` diff --git a/examples/official-site/content/components/download/examples/02-pdf.md b/examples/official-site/content/components/download/examples/02-pdf.md new file mode 100644 index 000000000..522795d46 --- /dev/null +++ b/examples/official-site/content/components/download/examples/02-pdf.md @@ -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; +``` diff --git a/examples/official-site/content/components/download/examples/03-image-blob.md b/examples/official-site/content/components/download/examples/03-image-blob.md new file mode 100644 index 000000000..60aebdd87 --- /dev/null +++ b/examples/official-site/content/components/download/examples/03-image-blob.md @@ -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. diff --git a/examples/official-site/sqlpage/migrations/01_documentation.sql b/examples/official-site/sqlpage/migrations/01_documentation.sql index f0d138803..099d087be 100644 --- a/examples/official-site/sqlpage/migrations/01_documentation.sql +++ b/examples/official-site/sqlpage/migrations/01_documentation.sql @@ -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"}]')); diff --git a/examples/official-site/sqlpage/migrations/65_download.sql b/examples/official-site/sqlpage/migrations/65_download.sql deleted file mode 100644 index a05b49ab8..000000000 --- a/examples/official-site/sqlpage/migrations/65_download.sql +++ /dev/null @@ -1,140 +0,0 @@ --- Insert the download component into the component table -INSERT INTO - component (name, description, icon, introduced_in_version) -VALUES - ( - 'download', - ' -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. -', - 'download', - '0.37.0' - ); - --- Insert the parameters for the download component into the parameter table -INSERT INTO - parameter ( - component, - name, - description, - type, - top_level, - optional - ) -VALUES - ( - 'download', - 'data_url', - '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.', - 'TEXT', - TRUE, - FALSE - ), - ( - 'download', - 'filename', - '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.', - 'TEXT', - TRUE, - TRUE - ); - --- Insert usage examples of the download component into the example table -INSERT INTO - example (component, description) -VALUES - ( - 'download', - ' -## 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; -``` -' - ), - ( - 'download', - ' -## 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; -``` -' - ), - ( - 'download', - ' -## 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. -' - ); \ No newline at end of file diff --git a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql index 2c198e598..dfa7865bf 100644 --- a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql +++ b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql @@ -30,8 +30,6 @@ WITH copy(name, category, summary) AS (VALUES - ('csv', 'http-responses', 'Export query rows as a downloadable CSV file.'), - ('download', 'http-responses', 'Send a file directly to the visitor.'), ('json', 'http-responses', 'Return query results as JSON for an API.'), ('rss', 'http-responses', 'Publish an RSS feed from query results.') diff --git a/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql b/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql deleted file mode 100644 index e23572da4..000000000 --- a/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql +++ /dev/null @@ -1,23 +0,0 @@ --- Every component whose `id` and `class` properties behave in the ordinary way --- is documented from the lists below, so that the wording stays identical --- across components and so that the (component, top_level, name) primary key --- of `parameter` is not violated. --- --- A handful of components deliberately document `id` or `class` in their own --- migration instead, because the generic wording would be wrong for them: --- `modal` (`id` is required, and is what a button targets), `form` (`id` is --- what an outside submit button references), `foldable` (top level and item --- level mean different things) and `button` (`id` lands on each button, not on --- the container). Do not duplicate those here. - -INSERT INTO parameter(component, top_level, name, description, type, optional) -SELECT *, 'id', 'id attribute added to the container in HTML. It can be used to target this item through css or for scrolling to this item through links (use "#id" in link url).', 'TEXT', TRUE -FROM (VALUES - ('csv', TRUE) -); - -INSERT INTO parameter(component, top_level, name, description, type, optional) -SELECT *, 'class', 'class attribute added to the container in HTML. It can be used to apply custom styling to this item through css. Added in v0.18.0.', 'TEXT', TRUE -FROM (VALUES - ('csv', TRUE) -);