How to enable a user to download a file in Fable?

247 Views Asked by At

I have a SAFE stack app. I need to enable users to upload and download files.

Uploading works by making use of

Browser.Dom.FileReader.Create()

Is there a corresponding way to enable users to download files?

This answer offers a solution using a completely different mechanism which depends on a js library. Is there no mechanism that corresponds to the FileReader approach?

1

There are 1 best solutions below

0
Chechy Levas On

I have come up with the following which seems to work for me.

let downLoad fileName fileContent =
    let anchor = Browser.Dom.document.createElement "a"
    let encodedContent = fileContent |> sprintf "data:text/plain;charset=utf-8,%s" |> Fable.Core.JS.encodeURI
    anchor.setAttribute("href",  encodedContent)
    anchor.setAttribute("download", fileName)
    anchor.click()

Related Questions in F#

Related Questions in FABLE-F#