The file object (MDN) has a method text that returns the file contents as a Promise<string>. This method appears to be missing from the Fable bindings. However, we can use the ? operator from Fable.Core.JsInterop to access it.
file?text()
In Elmish, we wrap promises inside of commands in order to keep the application logic pure.
First we introduce a message type:
type Message =
| FileDropped of Browser.Types.File
| FileContent of Result<string, Exception>
FileDropped is dispatched when the user drops a file onto the drop zone. FileContent is dispatched when the content of a file is ready.
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: