Skip to main content

File

This page describes functions for interacting with file repositories, for example to create, read and list files.

File repositories can be created via the Accounts tab. There are three types of file repositories:

  • Session-scoped temporary storage
  • Public permanent storage
  • Private permanent storage

The example Upload files via form illustrates how to use session-scoped temporary storage in web forms.

Permanent storage can either be public or private. When it is public, files can be accessed via publicly available URLs. This can be used, for example, for storing static assets such as images and JS files that power a web application.

file_create

Creates a file in a file repository.

file_create(file_account, file_name, file_data);

Example :

let file_account = "file-rep";
let file_name = "test_file.txt";
let file_data = "data";

file_create(file_account, file_name, file_data);
let file_content = file_read(file_account, file_name);
log("file content:", file_content);

file_append

Appends data to a file in a file repository.

file_append(file_account, file_name, append_data);

Example :

let file_account = "file-rep";
let file_name = "test_file.txt";
let file_data = "data";

file_append(file_account, file_name, file_data);
let file_content = file_read(file_account, file_name);
log("file content:", file_content);

file_list

Lists files in a file repository.

Returns an array of filename strings.

let list = file_list(file_account);

Example :

let file_account = "file-rep";

let list = file_list(file_account);
log("list :", list);

file_remove

Removes a file from a file repository.

file_remove(file_account, file_name);

Example :

let file_account = "file-rep";
let file_name = "test_file.txt";

file_remove(file_account, file_name);

file_exists

Checks if a file exists in a file repository.

Returns true if the file exists, false otherwise.

let exists = file_exists(file_account, file_name);

Example :

let file_account = "file-rep";
let file_name = "test_file.txt";

let exists = file_exists(file_account, file_name);
log("exists:", exists);

repository_clear

Clears all files from a file repository. Does not work for session-scoped repositories.

repository_clear(file_account);

Example :

let file_account = "file-rep";
repository_clear(file_account);

file_read

Reads the content of a file from a file repository.

Returns the file content as a string.

Do not use this for large files.

let content = file_read(file_account, file_name);

Example :

let file_account = "file-rep";
let file_name = "test_file.txt";

let content = file_read(file_account, file_name);
log("content:", content);

file_copy

Copies a file within a file repository, or between two repositories. With this command it is possible to copy a file from one repository to an other using the optional target_file_account parameter. Be careful - if there are only three parameters the third parameter is taken as target_file_name.

file_copy(source_file_account, source_file_name, [target_file_account], target_file_name);

Example 1:

let file_account = "file-rep";
let source_file_name = "source_test_file.txt";
let target_file_name = "target_test_file.txt";

file_copy(file_account, source_file_name, target_file_name);

Example 2:

let source_file_account = "file-rep_1";
let source_file_name = "source_test_file.txt";
let target_file_account = "file-rep_2";
let target_file_name = "target_test_file.txt";

file_copy(source_file_account, source_file_name, target_file_account, target_file_name);

file_rename

Renames a file in a file repository.

file_rename(file_account, source_file_name, target_file_name);

Example 1:

let file_account = "file-rep";
let source_file_name = "source_test_file.txt";
let target_file_name = "target_test_file.txt";

file_rename(file_account, source_file_name, target_file_name);

file_enable_session_upload

Use this in a package backing a service to enable file upload for the current browser session.
Once this is enabled, files for the current session can be uploaded to all accounts with account type "File repository" and repository type "Session-scoped temporary storage".

file_enable_session_upload();
const $uploadUrl = `/filerepository/<your-profile>/<your-environment>/<your-file-repo>`;

For a complete example, see Upload files via form.

file_disable_session_upload

Use this is a package backing a service to disable file upload for the current browser session.

file_disable_session_upload();

For a complete example, see Upload files via form.

file_upload_enabled

Checks if session upload is enabled.

Returns true if enabled, false otherwise.

file_upload_enabled();

stream_to_archive

Streams multiple files from a list of sources into a ZIP archive in a file repository. Archive is created or replaced if existing.

stream_to_archive(archive_name, list_of_sources, filestore);

The list of sources list elements must contain name of the file, path inside of the archive, and one of url, buffer or plain text.

let list_of_sources = [
{
// Sample identified by url like from Xurrent attachment or from a s3 presigned url
url: "https://s3.sample_url", // url to download as an alternative to buffer
name: "streamed_from_s3.txt",
path: "sample", // path inside the archive
},
{
// Sample from a buffer like from createpdf
buffer: pdf_result, // buffer to add as an alternative to url
name: "sample.pdf",
path: "pdfs", // path inside the archive
},
{
// Sample from a text
text: "Hello World!", // text to add as an alternative to url
name: "simple.txt",
path: "texts", // path inside the archive
},
];

stream_to_file

Streams the content from a URI into a file in a file repository.

stream_to_file(filestore, filename, uri);

stream_to_uri

Streams a file from a file repository to a presigned URL.

stream_to_uri(filestore, filename, uri, [method], [content_type]);

Default value for method is PUT. Some services need POST.