2014-07-21 31 views
5

以下代碼片段位於FSharp.Data網站http://fsharp.github.io/FSharp.Data/library/Http.html中。 TextBinary的類型分別是stringbyte[]。將整個2GB文件存入內存並將其保存到文件並不好。如何使用FSharp.Data的http模塊下載大文件?

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

+0

不錯,它需要檢查RequestStream和ResponseStream的返回'Response'對象是否爲空? – ca9163d9

+1

考慮到這是一個F#特定的庫,我猜不。 – Daniel

回答

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 

如果你想嘗試下載無限文件檢查the complete source(在你自己的風險運行,它使用的是The Infinite File Download

+1

AwaitTaskVoid在這裏http://stackoverflow.com/questions/8022909/how-to-async-awaittask-on-plain-task-not-taskt –