#mizchi/js_browser/file

    File API for file handling in the browser.

    #Installation

    Add to your moon.mod:

    import { "mizchi/js_browser@0.13.0", }

    ...and to the moon.pkg of the package that uses it:

    import { "mizchi/js_browser/file", }

    #Overview

    Provides bindings for the File and FileReader APIs for reading files selected by users.

    #Usage Example

    fn main {
    // Get file input element
    let doc = @dom.document()
    let input = doc.get_element_by_id("fileInput")

    // Access selected files
    let files = input.files()
    let file = files.item(0)

    // Read file as text
    let reader = @file.FileReader()

    reader.set_onload(fn(event) {
    let result = reader.result()
    @console.log("File content:", result)
    })

    reader.read_as_text(file)

    // Other reading methods:
    // reader.read_as_data_url(file)
    // reader.read_as_array_buffer(file)
    }

    #Available Types

    • File - File object with name, size, type
    • FileReader - Read file contents
    • FileList - List of files from input

    #Reading Methods

    • readAsText() - Read as text string
    • readAsDataURL() - Read as data URL (for images)
    • readAsArrayBuffer() - Read as binary data

    #Reference

    File

    pub(all) struct File {
    name : String
    lastModified : Int
    size : Int
    }

    File represents a file object, extending Blob

    File::File

    fn File::File(data : Array[String], filename : String, content_type? : String, last_modified? : Int) -> File

    Create a new File

    File::arrayBuffer

    #alias(array_buffer)
    async fn File::arrayBuffer(self : File) ->
    ArrayBuffer

    Get the file contents as ArrayBuffer (inherited from Blob)

    File::as_any

    fn File::as_any(self : File) ->
    Any

    File::as_blob

    fn File::as_blob(self : File) ->
    Blob

    Cast File to Blob

    File::contentType

    #alias(content_type)
    fn File::contentType(self : File) -> String

    Get the content type of the File

    File::new

    #deprecated("use `File(..)` instead")
    fn File::new(data : Array[String], filename : String, content_type? : String, last_modified? : Int) -> File

    Deprecated alias for the File(..) constructor.

    File::slice

    fn File::slice(self : File, start? : Int, end? : Int) ->
    Blob

    Create a new File containing a slice of this File (inherited from Blob)

    File::text

    async fn File::text(self : File) -> String

    Get the file contents as text (inherited from Blob)

    FileReader

    FileReader allows reading file contents asynchronously

    FileReader::FileReader

    fn FileReader::FileReader() -> FileReader

    Create a new FileReader

    FileReader::abort

    fn FileReader::abort(self : FileReader) -> Unit

    Abort the read operation

    FileReader::as_any

    FileReader::new

    #deprecated("use `FileReader(..)` instead")
    fn FileReader::new() -> FileReader

    Deprecated alias for the FileReader(..) constructor.

    FileReader::readAsArrayBuffer

    #alias(read_as_array_buffer)
    fn FileReader::readAsArrayBuffer(self : FileReader, blob :
    Blob
    ) -> Unit

    Read file as ArrayBuffer

    FileReader::readAsDataURL

    #alias(read_as_data_url)
    fn FileReader::readAsDataURL(self : FileReader, blob :
    Blob
    ) -> Unit

    Read file as Data URL

    FileReader::readAsText

    #alias(read_as_text)
    fn FileReader::readAsText(self : FileReader, blob :
    Blob
    , encoding? : String) -> Unit

    Read file as text with optional encoding

    FileReader::set_onerror

    fn FileReader::set_onerror(self : FileReader, callback : (
    Any
    ) -> Unit) -> Unit

    Set onerror event handler

    FileReader::set_onload

    fn FileReader::set_onload(self : FileReader, callback : (
    Any
    ) -> Unit) -> Unit

    Set onload event handler

    FileReader::set_onloadend

    fn FileReader::set_onloadend(self : FileReader, callback : (
    Any
    ) -> Unit) -> Unit

    Set onloadend event handler

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io