diff options
Diffstat (limited to 'elements')
-rwxr-xr-x | elements/dune | 2 | ||||
-rwxr-xr-x | elements/transfert.ml | 22 |
2 files changed, 24 insertions, 0 deletions
diff --git a/elements/dune b/elements/dune index 755bd05..97d0753 100755 --- a/elements/dune +++ b/elements/dune @@ -3,5 +3,7 @@ (libraries brr brr.note + js_of_ocaml ) + (preprocess (pps ppx_hash js_of_ocaml-ppx)) ) diff --git a/elements/transfert.ml b/elements/transfert.ml new file mode 100755 index 0000000..ddeecd0 --- /dev/null +++ b/elements/transfert.ml @@ -0,0 +1,22 @@ +open Js_of_ocaml +open Brr + +let send + : mime_type:Jstr.t -> filename:Jstr.t -> Jstr.t -> unit + = fun ~mime_type ~filename content -> + let btoa = Jv.get Jv.global "btoa" in + let base64data = Jv.apply btoa + [| Jv.of_jstr content |] in + + let mime = (Jv.Id.(of_jv @@ to_jv mime_type)) + and base64 = (Jv.Id.(of_jv @@ to_jv base64data)) + in + + let data = (Js.string "data:")##concat_3 mime (Js.string ";base64,") base64 in + + (* Create the link to download the the element, and simulate a click on it *) + let a = El.a + ~at:At.[ href Jv.Id.(of_jv @@ to_jv data) + ; v (Jstr.v "download") filename ] + [] in + El.click a |