如何允许用户在Fable中下载文件?



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

使用

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

是否有相应的方式让用户下载文件?

这个答案提供了一个使用完全不同的机制的解决方案,它依赖于js库。是否没有对应于FileReader方法的机制?

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

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

最新更新