Skip to content

Instantly share code, notes, and snippets.

@mbomhoff
Created July 12, 2018 23:03
Show Gist options
  • Save mbomhoff/ddc0ebc1330bd70e2f66cba1f016c852 to your computer and use it in GitHub Desktop.
Save mbomhoff/ddc0ebc1330bd70e2f66cba1f016c852 to your computer and use it in GitHub Desktop.
Client-side file download in elm
module Main exposing (..)
import Html exposing (Html, Attribute, text, div, textarea, a, button)
import Html.App exposing (beginnerProgram)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import Http exposing (uriEncode)
main =
beginnerProgram { model = "", view = view, update = update }
type Msg
= NewContent String
update (NewContent content) oldContent =
content
view content =
div []
[ textarea
[ placeholder "Text to download ...."
, onInput NewContent
, rows 10
, cols 30
]
[]
, div []
[ a
[ type' "button"
, href <| "data:text/plain;charset=utf-8," ++ uriEncode content
, downloadAs "demo.txt"
]
[ button [] [ text "Download" ] ]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment