Created
October 31, 2014 11:13
-
-
Save mmport80/68d142e346cf906f4d9d to your computer and use it in GitHub Desktop.
File Upload Port Example for 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 FileUpload where | |
import String | |
port output : Signal [String] | |
port output = String.lines <~ openFromFile | |
port openFromFile : Signal String |
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
<html> | |
<head> | |
<script type="text/javascript" src="http://elm-lang.org/elm-runtime.js"></script> | |
<script type="text/javascript" src="build/FileUpload.js"></script> | |
</head> | |
<body> | |
<p> | |
File upload using Elm ports | |
<p> | |
<input type="file" id="fileinput"/> | |
</body> | |
<script type="text/javascript" src="Ports.js"></script> | |
</html> |
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
//text output -- it no html or graphical output | |
var fileupload = Elm.worker(Elm.FileUpload, { | |
openFromFile: "" | |
}); | |
var upload = document.getElementsByTagName('input')[0] | |
upload.onchange = function (e) { | |
reader = new FileReader(); | |
reader.onload = function (event) { | |
data = event.target.result; | |
//file's text data is sent to 'openfromfile' port | |
fileupload.ports.openFromFile.send(data); | |
} | |
reader.readAsText(upload.files[0]); | |
}; | |
function logger(x) { console.log(x) } | |
//data from 'output' port is sent to logger function | |
fileupload.ports.output.subscribe(logger); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment