I have a Elmish-React project and this dropzone
OnDrop (fun e ->
let file = e.dataTransfer.files.item 0
e.preventDefault()
e.stopPropagation()
)
how do I load the content of this file?
I have a Elmish-React project and this dropzone
OnDrop (fun e ->
let file = e.dataTransfer.files.item 0
e.preventDefault()
e.stopPropagation()
)
how do I load the content of this file?
The
file
object (MDN) has a methodtext
that returns the file contents as aPromise<string>
. This method appears to be missing from the Fable bindings. However, we can use the?
operator fromFable.Core.JsInterop
to access it.In Elmish, we wrap promises inside of commands in order to keep the application logic pure.
First we introduce a message type:
FileDropped
is dispatched when the user drops a file onto the drop zone.FileContent
is dispatched when the content of a file is ready.Here is the command for loading file content:
And here is a complete working example of how to load the contents of a drag-and-dropped file and display it in an Elmish view.
Note that the following packages were used: