3

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?

Chechy Levas
  • 2,206
  • 1
  • 13
  • 28

1 Answers1

3

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()
Chechy Levas
  • 2,206
  • 1
  • 13
  • 28