• Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    Elm
  • License
    BSD 3-Clause "New...
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Select files. Download files. Work with file content.

Files

Select files. Download files. Work with file content.

Maybe you generate an SVG floorplan or a PDF legal document? You can use File.Download to save those files to disk. Maybe you want people to upload a CSV of microbe data or a JPG of their face? You can use File.Select to get those files into the browser.

This package does not allow arbitrary access to the file system though. Browsers restrict access to the file system for security. Otherwise, any website on the internet could go try to read private keys out of ~/.ssh or whatever else they want!

Download Example

Maybe you want people to download the floorplan they just designed as an SVG file? You could use File.Download.string like this:

import File.Download as Download

download : String -> Cmd msg
download svgContent =
  Download.string "floorplan.svg" "image/svg+xml" svgContent

Upload Example

Maybe you want to help scientists explore and visualize data? Maybe they need to upload CSV files like this:

Name,Age,Weight,Height
Tom,22,63,1.8
Sue,55,50,1.6
Bob,35,75,1.85

You could use File.Select.file and File.toString to create a program like this:

import Browser
import File exposing (File)
import File.Select as Select
import Html exposing (Html, button, p, text)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
import Task



-- MAIN


main : Program () Model Msg
main =
  Browser.element
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }



-- MODEL


type alias Model =
  { csv : Maybe String
  }


init : () -> (Model, Cmd Msg)
init _ =
  ( Model Nothing, Cmd.none )



-- UPDATE


type Msg
  = CsvRequested
  | CsvSelected File
  | CsvLoaded String


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    CsvRequested ->
      ( model
      , Select.file ["text/csv"] CsvSelected
      )

    CsvSelected file ->
      ( model
      , Task.perform CsvLoaded (File.toString file)
      )

    CsvLoaded content ->
      ( { model | csv = Just content }
      , Cmd.none
      )



-- VIEW


view : Model -> Html Msg
view model =
  case model.csv of
    Nothing ->
      button [ onClick CsvRequested ] [ text "Load CSV" ]

    Just content ->
      p [ style "white-space" "pre" ] [ text content ]



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions _ =
  Sub.none

From there you could parse the CSV file, start showing scatter plots, etc.

More Repositories

1

compiler

Compiler for Elm, a functional language for reliable webapps.
Haskell
7,493
star
2

core

Elm's core libraries
Elm
2,791
star
3

elm-lang.org

Server and client code for the Elm website.
Elm
1,987
star
4

projects

Curated collection of projects for folks looking to collaborate within the Elm ecosystem.
423
star
5

html

Use HTML in Elm!
Elm
395
star
6

browser

Create Elm programs that run in browsers!
Elm
311
star
7

package.elm-lang.org

website for browsing packages and exploring documentation
Haskell
293
star
8

parser

A parsing library, focused on simplicity and great error messages
Elm
226
star
9

virtual-dom

The foundation of HTML and SVG in Elm.
JavaScript
208
star
10

error-message-catalog

A catalog of broken Elm programs / data to improve error messages
Elm
173
star
11

http

Make HTTP requests in Elm
Elm
155
star
12

svg

Fast SVG in Elm
Elm
146
star
13

time

A simplified approach to working with dates, times, and time zones.
Elm
92
star
14

json

Work with JSON in Elm
Elm
90
star
15

bytes

Work with bytes and implement network protocols
Elm
80
star
16

url

Build and parse URLs. Useful for HTTP and "routing" in single-page apps (SPAs)
Elm
75
star
17

expectations

Trying to set expectations about @elm processes
53
star
18

editor-plugins

List of editor plugins for Elm.
50
star
19

random

Generate random values in Elm
Elm
45
star
20

regex

If you really need regex in Elm, it is possible.
Elm
33
star
21

color

Standard representation of colors, encouraging sharing between packages.
Elm
24
star
22

foundation.elm-lang.org

Source code for the ESF website
Elm
17
star
23

project-metadata-utils

Work with elm.json and docs.json files in Elm
Elm
16
star
24

forum-rules

Rules for Elm forums
9
star