norpan/elm-file-reader/2.0.0

NOTE: this package is not compatible with Elm 0.19.1

FileReader

This module exposes attributes that you can use to read files into Elm.

type alias File =
{ lastModified : Time
, name : String
, size : Int
, mimeType : String
, dataFormat : DataFormat
, data : Result Error String
}

The result of a file reading operation

= DataURL
| Base64
| Text String

The format you want your data in. DataURL - a data URL as specified by RFC 2397. Base64 - Base64 encoded as specified by RFC 4648 Text encoding - Text, encoded using the specified encoding (See https://www.w3.org/TR/encoding/#concept-encoding-get)

type alias Error =
{ code : Int
, name : String
, message : String
}

An error returned from file reading. This corresponds to a DomException (name and message) or a FileError (code) gotten when trying to read the file.

fileInput : DataFormat -> (File -> msg) -> List (Attribute msg)

Add these attributes to an input node to get the contents of the selected file.

type Msg
    = FileSelected File


view =
    Html.input (fileInput DataURL FileSelected) []
filesInput : DataFormat -> (List File -> msg) -> List (Attribute msg)

Add these attributes to an input node to get the contents of the selected files.

type Msg
    = FilesSelected (List File)


view =
    Html.input (filesInput DataURL FilesSelected) []
{ dataFormat : DataFormat
, enterMsg : msg
, leaveMsg : msg
, filesMsg : List File -> msg
}
-> List (Attribute msg)

Add these attributes to any node to create a "drop zone" for files and get the contents of the dropped files. The enterMsg and leaveMsg messages could be used to visually indicate the drop zone.

You will either get a leaveMsg or a filesMsg, so make sure the filesMsg also clears any visual indication started by the enterMsg.

type Msg
    = EnterDropZone
    | LeaveDropZone
    | FilesDropped (List File)


view =
    Html.div
        (dropZone
            { dataFormat = Base64
            , enterMsg = EnterDropZone
            , leaveMsg = LeaveDropZone
            , filesMsg = FilesDropped
            }
        )
        [ Html.text "Drop files here" ]