首页 > 解决方案 > 下载寓言文件

问题描述

如何下载寓言中的文件?假设我有一个 CSV 数据,我想将它保存在我的机器上。

我尝试ts2fableDownload.js. 但是,它只创建一个接口:

// ts2fable 0.6.1
module rec Download

open Fable.Import

type [<AllowNullLiteral>] IExports =
    abstract downloadUrl: title: string * url: string -> unit
    abstract downloadBlob: title: string * blob: Browser.Blob -> unit
    abstract downloadText: title: string * content: string -> unit

我认为有一种方法可以直接导入 .js 模块

let download = Fable.Core.JsInterop.importMember "../src/download.js"

但它看起来并不漂亮和优雅。

标签: f#fable-f#

解决方案


您可以使用importFable 中的函数以便在 JavaScript 中使用相同的语法。

所以在你的情况下,它会给出类似的东西:

// If the file is in your src folder
let download : Download.IExports = import "*" "./path/to/download.js"

// If you use npm to manage the library
let download : Download.IExports = import "*" "download.js"

你可以使用这样的代码:

let anchor = Browser.document.createElement("a") :?> Browser.HTMLAnchorElement
anchor.innerText <- "Test download"

anchor.onclick <- fun _ ->
    download.downloadText("test.txt", "Hello, this is the content of the file.")

Browser.document.body.appendChild(anchor)
|> ignore

有关信息,我在 Firefox、Chrome 和 Safari 下测试了代码。它适用于 Chrome 和 Safari,但在 Firefox 下没有任何反应。我不知道这是 Firefox 还是库的限制。


推荐阅读