Created
July 12, 2018 23:03
-
-
Save mbomhoff/ddc0ebc1330bd70e2f66cba1f016c852 to your computer and use it in GitHub Desktop.
Client-side file download in elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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