[Haskell-beginners] Download a large file using Network.HTTP

Felipe Almeida Lessa felipe.lessa at gmail.com
Mon Dec 10 14:45:56 CET 2012


Hey!

It's probably going to be easier to use http-conduit.  As the docs of
httpLbs say,

     If you want more power, such as interleaved actions on the
response body during download, you'll need to use 'http' directly.

So you'll need to use the 'http' function.  On the response headers
you'll find the total size of the download.  With that size in hand,
you may implement a Conduit (from the conduit package)

    type TotalSize = Int
    findTotalSize :: ResponseHeaders -> Maybe TotalSize
    showProgress :: MonadIO m => Maybe TotalSize -> Conduit ByteString
m ByteString

All data will pass through this conduit, so it may just keep track of
how many bytes it has seen already and show the progress while
returning the data itself unaltered.  Then you'll just need a Sink
that saves the file (e.g. sinkFile from Data.Conduit.Binary) and to
connect everything, e.g.

    res <- http request manager
    let mtotalSize = findTotalSize (responseHeaders res)
    responseBody res $= showProgress mtotalSize $$ sinkFile "output"

Cheers,

On Mon, Dec 10, 2012 at 11:34 AM, Cedric Fung <root at vec.io> wrote:
> Hi,
>
> Are there any suggestions to download a large file with Haskell? I have read
> the docs for Network, Network.HTTP and Network.HTTP.Conduit, but can't find
> anything which fit my requirements.
>
> I want to download a large file from an HTTP URL, and show the progress
> instantly. Maybe some functions which read HTTP connection and return a lazy
> ByteString could do this work?
>
> Though I found a low-level socket lazy package, which seems to work, I just
> want a more high level API.
>
> Thanks and regards.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Felipe.



More information about the Beginners mailing list