如何使用户能够下载 Fable 中的文件?

时间:2021-01-16 12:27:18

标签: f# fable-f# elmish

我有一个安全堆栈应用程序。我需要允许用户上传和下载文件。

利用

上传作品
Browser.Dom.FileReader.Create()

有没有相应的方法可以让用户下载文件?

This answer 提供了一种使用完全不同的机制的解决方案,该机制依赖于 js 库。有没有对应于 FileReader 方法的机制?

1 个答案:

答案 0 :(得分:2)

我想出了以下似乎对我有用的方法。

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()
相关问题