下载寓言中的文件 [英] Download file in Fable

查看:42
本文介绍了下载寓言中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I download file in Fable? Let's say I have a CSV data and I want to save it on my machine.

我尝试将 ts2fable Download.js 一起使用.但是,它仅创建一个接口:

I tried using ts2fable with the Download.js. However, it creates just an interface:

// 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模块的方法

I think there is a way directly import a .js module

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

但是它看起来并不优雅.

But it doesn't look nice and elegant.

推荐答案

您可以在Fable中使用 import 函数,以便在JavaScript中使用相同的语法.

you can use the import function in Fable in order to use the same syntax in JavaScript.

因此,在您的情况下,它会显示以下内容:

So in your case, it would give something like:

// 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或库的限制.

For information, I tested the code under Firefox, Chrome, and Safari. It works for Chrome and Safari but nothing happens under Firefox. I don't know if this is a limitation of Firefox or the library.

这篇关于下载寓言中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆