如何使用FSharp.Data的http模块下载大文件?

6
以下代码片段可在 FSharp.Data 网站 http://fsharp.github.io/FSharp.Data/library/Http.html 找到。其中,TextBinary 的类型分别为 stringbyte[]。不建议将整个2GB文件读入内存并保存到文件中。
let logoUrl = "https://raw.github.com/fsharp/FSharp.Data/master/misc/logo.png"
match Http.Request(logoUrl).Body with
| Text text -> 
    printfn "Got text content: %s" text
| Binary bytes -> 
    printfn "Got %d bytes of binary content" bytes.Length

4
Http.RequestStream(logoUrl).ResponseStream.CopyTo(outStream) 看起来可以实现这个功能。 - Daniel
很好,需要检查RequestStreamResponseStream返回的Response对象是否为空吗? - ca9163d9
1
考虑到这是一个特定于F#的库,我猜想不行。 - Daniel
1个回答

2

我认为您不能像在FSharp.Data网站上那样保留相同的代码来下载大文件。 我用于下载大文件的是

async {
    let! request = Http.AsyncRequestStream(logoUrl)
    use outputFile = new System.IO.FileStream(fileName,System.IO.FileMode.Create)
    do! request.ResponseStream.CopyToAsync( outputFile ) |> Async.AwaitTaskVoid 
} |> Async.RunSynchronously

如果您想尝试下载无限文件,请检查完整源代码(自行承担风险,它使用了无限文件下载)。

1
AwaitTaskVoid在这里:https://dev59.com/Mmsz5IYBdhLWcg3wQFe2 - Dzmitry Lahoda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接