简体   繁体   中英

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

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM