Try Install Learn Blog API Packages GitHub
Pages

File

Functions for getting, creating and reading files in different formats.

Functions

fromString
(
contents
:
String
name
:
String
type
:
String
)
:
File

Creates a new file from the contents, name and mime-type.

File.fromString("Some contents...", "test.txt", "text/plain")
mimeType
(
file
:
File
)
:
String

Returns the mime type of the file.

(File.fromString("Some contents...", "test.txt", "text/plain")
|> File.mimeType()) == "text/plain"
name
(
file
:
File
)
:
String

Returns the name of the file.

(File.fromString("Some contents...", "test.txt", "text/plain")
|> File.name()) == "test.txt"
readAsDataURL
(
file
:
File
)
:
Promise(Never, String)

Reads the contents of the given file as a Data URL.

sequence {
  file =
    File.fromString("Some content...", "test.txt", "text/plain")

  url =
    File.readAsDataURL(file)

  url == "data:text/plain;...."
}
readAsString
(
file
:
File
)
:
Promise(Never, String)

Reads the contents of the given file as a String.

sequence {
  file =
    File.create("Some content...", "test.txt", "text/plain")

  url =
    File.readAsString(file)

  url == "Some content..."
}
select
(
accept
:
String
)
:
Promise(Never, File)

Opens the browsers file dialog for selecting a single file.

  • The mime type can be restricted to the given one.

  • It might not esolve if the user cancels the dialog.

    sequence { file = File.select("application/json")

    Debug.log(file) }

selectMultiple
(
accept
:
String
)
:
Promise(Never, Array(File))

Opens the browsers file dialog for selecting multiple files.

  • The mime type can be restricted to the given one.

  • It might not resolve if the user cancels the dialog.

    sequence { files = File.selectMultiple("application/json")

    Debug.log(files) }

size
(
file
:
File
)
:
Number

Returns the size of the file in bytes.

(File.fromString("Some contents...", "test.txt", "text/plain")
|> File.size()) == 16