Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download big files using the http module of FSharp.Data?

Tags:

f#

f#-data

The following code snippet is found in the FSharp.Data website http://fsharp.github.io/FSharp.Data/library/Http.html. The type of Text and Binary are string and byte[] respectively. It's not good to get the whole 2GB file in memory and then save it to a file.

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
like image 580
ca9163d9 Avatar asked Jul 21 '14 17:07

ca9163d9


1 Answers

I do not think you can keep the same code as on the FSharp.Data website to download huge files. What I use to download large files is

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

If you want to try to download infinite file check the complete source (run on your own risk, it is using the The Infinite File Download)

like image 112
davidpodhola Avatar answered Oct 22 '22 12:10

davidpodhola