> For the complete documentation index, see [llms.txt](https://docs.ninox.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ninox.com/ninox-scripting/automate-your-workflows/work-with-functions/files-and-export.md).

# Files and export

Files help you move data into and out of Ninox. You can import attachments, create text or spreadsheet exports, bundle files, generate links, and control who can access shared files and views. This chapter shows you the core file functions for common automation and reporting tasks.

In this chapter, you will learn how to:

* Generate file, record, and view links.
* Share and unshare files and saved views.
* Load file content as Base64 for APIs or embedded output.

<table><thead><tr><th width="214.921875">Function (A-Z)</th><th>Task</th></tr></thead><tbody><tr><td><code>file()</code></td><td>Return a single file reference</td></tr><tr><td><code>files()</code></td><td>Return multiple file references</td></tr><tr><td><code>fileUrl()</code></td><td>Return a link to a file</td></tr><tr><td><code>loadFileAsBase64()</code></td><td>Return file content as a Base64 string</td></tr><tr><td><code>loadFileAsBase64URL()</code></td><td>Return file content as a Base64 data URL</td></tr><tr><td><code>shareFile()</code></td><td>Create a share link for a file</td></tr><tr><td><code>shareView()</code></td><td>Create a share link for a view</td></tr><tr><td><code>unshareAllViews()</code></td><td>Remove sharing from all views of a table</td></tr><tr><td><code>unshareFile()</code></td><td>Stop sharing a file</td></tr><tr><td><code>unshareView()</code></td><td>Stop sharing one saved view</td></tr></tbody></table>

## Collect files

Use these functions when you want to work with one or more file objects in a script.

### Return one or more file objects with `file()` and `files()`

Use `file()` when you need one file reference. Use `files()` when you need the full list.

Use them when you want to:

* Read attached files from a record.
* Pass file objects to other file functions.
* Work with a single file or all available files, depending on the field content.
* Pick one specific attachment by filename from a record.

`file(any)`\
`file(nid, string)`\
`files(any)`\
`files(nid)`

* `any`: the file field, record, or supported source you want to read from
* `nid`: the record that contains the file attachment
* `string`: the exact filename of the attachment you want to return

`files()` returns an array of file objects.

#### Let’s take a look at some examples:

```ninox
files(this)
```

Returns all attachments of the current record as an array.

```ninox
count(files(this))
```

Returns the number of attachments on the current record.

```ninox
file(this, "My wanted document.pdf")
```

Returns the file attachment from the current record with the name "My wanted document.pdf".

Tips:

* Use `files()` when users can attach several files and you want all of them.
* Use `file()`  with the record ID and exact name of the file, when a record has several attachments and you need one exact file.
* Match the filename exactly when you use the record-and-name form.

## Inspect links and URLs

Use these functions when you want to inspect a file before you use it or create a link to a file or record.

### Create a link with `fileUrl()`

Use `fileUrl()` to return a URL for a file.

Use it when you want to:

* Show or send a download link.
* Return a file link after export.
* Pass a file URL into another process.
* Link to one specific attachment on a record.

`fileUrl(file)`\
`fileUrl(nid, string)`

* `file`: the file object for which you want a link
* `nid`: the record that contains the file attachment
* `string`: the exact filename of the attachment you want to link to

`fileUrl()` returns a link.

#### Let’s take a look at some examples:

```ninox
let f := file(this, "My wanted document.pdf");
fileUrl(f)
```

Returns a link to the file.

```ninox
let fileName := extractx(text(Image), "[^/]+$");
fileUrl(this, fileName)
```

Extracts the filename from the `File` field and returns a link to that specific file.

Tips:

* Use the record-and-name form of `fileUrl()` when a record has several attachments and you need one exact link.
* `fileUrl()` is designed for client-side use.
* Do not use `fileUrl()` in triggers or inside `do as server`, `do as transaction`, or `do as deferred` blocks.
* The generated link uses information from the currently logged-in user, so it is not available in server-side execution.

## Share and unshare files

### Share or unshare a file with `shareFile()` and `unshareFile()`

Use `shareFile()` to create a share link for a file. Use `unshareFile()` to stop sharing it.

Use them when you want to:

* Generate a shareable link for an export or attachment.
* Store a file share URL in a text or URL field.
* Revoke a file link that should no longer be available.

`shareFile(file)`\
`shareFile(nid, string)`\
`unshareFile(file)`

* `file`: the file object you want to share or unshare
* `nid`: the record that contains the file you want to share
* `string`: the exact filename you want to share when you use the record form

`shareFile()` returns a link.

#### Let’s take a look at some examples:

```ninox
let x := createXLSX("orders.xlsx", select Orders);
shareFile(x)
```

Returns a share link for the exported file.

```ninox
shareFile(this, "NinoxLogo.jpg")
```

Returns a share link for the file `NinoxLogo.jpg` on the current record.

```ninox
let x := createXLSX("orders.xlsx", select orders);
unshareFile(x)
```

Stops sharing the file.

Tips:

* `shareFile()` creates a share link, not a user-specific file permission.
* Save the returned link in a URL or text field when you want to reuse it later.
* `shareFile()` does not work in a formula field because it changes data.
* Share links are tied to the database where they were created.
* If that original database is deleted after restore or migration, old share links stop working.
* Share only the final version of an export.

## Share and unshare saved views

Use these functions when you want to expose or remove access to a saved view.

### Manage shared views with `shareView()`, `unshareView()`, and `unshareAllViews()`

Use `shareView()` to share one saved view. Use `unshareView()` to stop sharing one view. Use `unshareAllViews()` to remove sharing from every shared view at once.

Use them when you want to:

* Publish a reporting view.
* Stop access to an outdated shared view.
* Reset shared view access in bulk.

`shareView(string)`\
`shareView(string, string)`\
`shareView(string, JSON)`\
`shareView(string, string, JSON)`\
`unshareView(string, string)`\
`unshareAllViews(string)`

* `string` (first argument of `shareView`): the table name
* `string` (second argument of the two-string form of `shareView`): the view name
* `JSON`: optional share settings, for example, `{ contentType: "pdf" }`
* `string` (first argument of `unshareView`): the table name
* `string` (second argument of `unshareView`): the saved view name
* `string` (argument of `unshareAllViews`): the table name

#### Let’s take a look at some examples:

```ninox
shareView("Events")
```

Returns a share link for the default view of the `Events` table.

```ninox
shareView("Events", "New York")
```

Returns a share link for the `New York` view of the `Events` table.

```ninox
shareView("Events", "San Francisco", {
    contentType: "pdf"
})
```

Returns a share link for the `San Francisco` view of the `Events` table as PDF.

```ninox
unshareView("Invoices", "Current month")
```

Stops sharing the `Current month` view of the `Invoices` table.

```ninox
unshareAllViews("Invoices")
```

Stops sharing all views of the `Invoices` table.

Tips:

* If you do not specify a view, Ninox uses the default view.
* `shareView()` returns a link that you can save in a URL or text field.
* `shareView()` supports formats such as `pdf`, `html`, `csv`, `xlsx`, and `json`.
* `shareView()` does not work in a formula field because it changes data.
* `unshareView()` does not work in a formula field because it changes data.
* `unshareAllViews()` does not work in a formula field because it changes data.
* Use stable table and view names in scripts.
* Review shared views regularly when data access matters.

## Load files as Base64 for APIs and embedding

Use these functions when you need the actual file content, not just the file object or a link.

### Return file content with `loadFileAsBase64()` and `loadFileAsBase64URL()`

Use `loadFileAsBase64()` to return raw Base64 text. Use `loadFileAsBase64URL()` when you need a ready `data:` URL.

Use them when you want to:

* Send file content to an API.
* Embed a file in generated output.
* Store binary file content as text for a follow-up request.
* Read one specific attachment from a record by filename.

`loadFileAsBase64(any)`\
`loadFileAsBase64(nid, string)`\
`loadFileAsBase64URL(any)`\
`loadFileAsBase64URL(nid, string)`

* `any`: the file object or supported file source you want to encode
* `nid`: the record that contains the file attachment
* `string`: the exact filename of the attachment you want to encode

`loadFileAsBase64()` returns a string. `loadFileAsBase64URL()` returns a string.

#### Let’s take a look at some examples:

```ninox
loadFileAsBase64("Attachment")
```

Returns the file content as a Base64 string.

```ninox
loadFileAsBase64(this, "myFoto.jpg")
```

Returns the attachment `myFoto.jpg` from the current record as a Base64 string.

```ninox
loadFileAsBase64(photo)
```

Returns the file from the `Photo` field as a Base64 string.

```ninox
loadFileAsBase64URL("Attachment")
```

Returns the file content as a `data:` URL.

```ninox
loadFileAsBase64URL(this, "myPhoto.jpg")
```

Returns the attachment `myPhoto.jpg` from the current record as a Base64 data URL.

```ninox
loadFileAsBase64URL(photo)
```

Returns the file from the `photo` field as a Base64 data URL.

Tips:

* Base64 increases the payload size.
* Use it only when the target system really needs inline content.
* Use the record-and-name form when a record has several attachments and you need one exact file.
* Use `loadFileAsBase64URL()` when the target expects a ready `data:` URL instead of raw Base64 text.

{% hint style="info" %}
File creation and sharing behavior can differ by client capabilities. Test export and sharing scripts in the environments your team uses most.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ninox.com/ninox-scripting/automate-your-workflows/work-with-functions/files-and-export.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
