Skip to content

Instantly share code, notes, and snippets.

@RblSb
Created May 30, 2017 22:52
Show Gist options
  • Save RblSb/89ddad18453ffb6c7ba007381cc00b58 to your computer and use it in GitHub Desktop.
Save RblSb/89ddad18453ffb6c7ba007381cc00b58 to your computer and use it in GitHub Desktop.
html5 file reference
class Test {
static function main() {
js.Browser.window.onclick = function() {
var input = js.Browser.document.createElement("input");
input.style.visibility = "hidden"; //comment this to test
input.setAttribute("type", "file");
input.id = "browse";
input.onclick = function(e) {
e.cancelBubble = true;
e.stopPropagation();
}
input.onchange = function() {
untyped var file = input.files[0];
var reader = new js.html.FileReader();
reader.readAsText(file);
reader.onload = function(evt) {
trace(evt.target.result);
js.Browser.document.body.removeChild(input);
}
}
js.Browser.document.body.appendChild(input);
input.click();
}
}
}
class Test {
static function main() {
js.Browser.window.ondragenter = function(e:js.html.DragEvent) {
e.preventDefault();
};
js.Browser.window.ondragover = function(e:js.html.DragEvent) {
e.preventDefault();
};
js.Browser.window.ondrop = function(e:js.html.DragEvent) {
var reader = new js.html.FileReader();
reader.onload = function(event) {
trace(haxe.Json.parse(event.target.result));
}
e.preventDefault();
reader.readAsText(e.dataTransfer.files[0]);
}
}
}
class Test {
static function main() {
var json = haxe.Json.stringify({a:1, b:"hi"});
var blob = new js.html.Blob([json], {
type: "application/json"
});
var url = js.html.URL.createObjectURL(blob);
var a = js.Browser.document.createElement('a');
js.Browser.document.body.appendChild(a);
untyped a.download = "example.json";
untyped a.href = url;
a.onclick = function(e) {
e.cancelBubble = true;
e.stopPropagation();
}
a.click();
js.Browser.document.body.removeChild(a);
js.html.URL.revokeObjectURL(url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment