createTextFile

To create a text file with specified name, content, and encoding options

The createTextFile function allows you to create and attach text files directly to records within Ninox. This function supports the creation of various file types, including .txt, .html, and .csv, which can then be downloaded or used outside of Ninox.

You can specify the file's name, extension, and content, and optionally define an encoding format (using the server-side variant). If no specific location is designated, the file will attach directly to the record, accessible via the paperclip icon (📎) in the Attachments tab.

Caution: The encoding option can only be used within a do as server code block.

Syntax

createTextFile(nid, string, string)

createTextFile(nid, string, string, JSON)

Parameters

  • record: The record to which the text file will be attached.

  • content: The text content to be written to the file. This can be:

    • Text directly entered within quotation marks. For example, "Hello, world!".

    • Text pulled from a text field, multiline text field, or rich text field.

  • filename: The intended file name, including its extension. For example, file.txt or document.html.

  • options (optional, server-side only): A JSON object specifying encoding options.

    • encoding: Defines the file's encoding format. Available encoding values:

      • 'utf8', 'utf-8'

      • 'utf16le', 'utf-16le' (aliases: 'usc2', 'usc-2')

      • 'latin1' (alias: 'binary')

      • 'base64'

      • 'base64url'

      • 'hex'

      • 'ascii'

Note: Encoding strings are not case-sensitive. For example, UTF-8 can be specified as utf8, UTF8, or uTf8.

Return

file: A generated file attached to the specified record.

Examples

In the following examples, your database contains these fields:

  • A rich text field named My text containing content to be saved.

  • An image field named File.

  • A button named Create document.

To follow these examples, open the settings of the Create document button, then add the corresponding script to the On click formula editor.

1. Basic text file attachment with plain text conversion

To create a text file named MyTextFileExample.txt using the plain text in My text, use:

createTextFile(this, text('My text'), "MyTextFileExample.txt")

Explanation: The text('My text') function removes any formatting and returns the plain text from My text.

Result: Upon clicking Create document, a .txt file named MyTextFileExample.txt is created and attached to the record. You can access the file in the Attachments tab (📎); clicking the image field will automatically download the file.

2. HTML file attachment with raw text (including HTML tags)

To create an HTML file (with HTML tags) named MyTextFileExample.html using the text from My text and attach it to the File image field, use:

File := createTextFile(this, raw('My text'), "MyTextFileExample.html")

Explanation: The raw('My text') function keeps any HTML tags within My text, allowing the output file to display as a formatted HTML document in a browser.

Result: Upon clicking Create document, an .html file named MyTextFileExample.html is created and attached to the File image field. Clicking the image field will automatically download the file.

Note: Using raw('My text') with an .html extension ensures that any HTML tags within My text (such as <h1>, <p>, etc.) are retained. This allows the file to open in a web browser with structured formatting based on these tags, which is suitable for content you wish to display as a styled web page.

3. HTML file with UTF-8 encoding

To apply UTF-8 encoding to an HTML file created from My text, use the server-side variant of the function within a do as server block:

do as server
  File := createTextFile(this, raw('My text'), "MyTextFileExample.html", {encoding: "utf8"})
end

Explanation: The server-side block (do as server ... end) is necessary to specify encoding.

Result: Upon clicking Create document, an .html file (encoded in UTF-8) named MyTextFileExample.html is created and attached to the File image field. Clicking the image field will automatically download the file.

Note: The encoding option is suitable for preserving international characters, special symbols, or multilingual content in the file.

See also

file, which returns a specific file based on the file name from a given record.

importFile, which imports a file from a URL and saves it as an attachment of a record.

Last updated