|
| 1 | +# Downloading Files Over HTTP |
| 2 | + |
| 3 | +The `Fake.Net.Http` module provides a functionality to download files over HTTP. |
| 4 | + |
| 5 | +[API-Reference](apidocs/fake-net-http.html) |
| 6 | + |
| 7 | +## Including the Fake.Net.Http dependency |
| 8 | + |
| 9 | +In order to open the `Fake.Net.Http` module from a build script you have to add a `Fake.Net.Http` dependency into your |
| 10 | +`paket.dependencies` file: |
| 11 | + |
| 12 | + group NetcoreBuild |
| 13 | + source https://api.nuget.org/v3/index.json |
| 14 | + |
| 15 | + nuget Fake.Net.Http prerelease |
| 16 | + |
| 17 | + .... other dependencies, like |
| 18 | + nuget Fake.Core.Target prerelease |
| 19 | + |
| 20 | +Please see more details on referencing FAKE 5 modules [here](apidocs/fake-fake5-modules.html). |
| 21 | + |
| 22 | +## Downloading a Single File |
| 23 | + |
| 24 | +To download a single file over HTTP use `downloadFile` from the Http module: |
| 25 | + |
| 26 | + open Fake.Net |
| 27 | + open Fake.Core |
| 28 | + |
| 29 | + Target.Create "DownloadFile" (fun _ -> |
| 30 | + let absoluteFilePath = Http.downloadFile "/tmp/5.zip" @"http://ipv4.download.thinkbroadband.com/5MB.zip" |
| 31 | + printfn "File path: %s" absoluteFilePath |
| 32 | + ) |
| 33 | + |
| 34 | +A console output should be: |
| 35 | + |
| 36 | + Downloading [http://ipv4.download.thinkbroadband.com/5MB.zip] ... |
| 37 | + Download succeeded |
| 38 | + File path: /tmp/5.zip |
| 39 | + |
| 40 | +## Downloading Multiple Files |
| 41 | + |
| 42 | +To download multiple files in parallel use `downloadFiles` from the Http module: |
| 43 | + |
| 44 | + open Fake.Net |
| 45 | + open Fake.Core |
| 46 | + |
| 47 | + Target.Create "DownloadFiles" (fun _ -> |
| 48 | + let files: Http.DownloadParameters list = [ |
| 49 | + {Path = "/tmp/5.zip"; Uri = "http://ipv4.download.thinkbroadband.com/5MB.zip"}; |
| 50 | + {Path = "/tmp/10.zip"; Uri = "http://ipv4.download.thinkbroadband.com/10MB.zip"}] |
| 51 | + let filePaths = Http.downloadFiles files |
| 52 | + printfn "File paths: %A" filePaths |
| 53 | + ) |
| 54 | + |
| 55 | +A console output should be: |
| 56 | + |
| 57 | + Downloading [http://ipv4.download.thinkbroadband.com/5MB.zip] ... |
| 58 | + Downloading [http://ipv4.download.thinkbroadband.com/10MB.zip] ... |
| 59 | + Download succeeded |
| 60 | + File paths: ["/tmp/5.zip"; "/tmp/10.zip"] |
| 61 | + |
| 62 | +## More Details |
| 63 | + |
| 64 | +* `downloadFile` and `downloadFiles` throw an Exception and fail a FAKE Target if any error occurs (invalid URI, invalid local file |
| 65 | + path/permissions, etc.) |
| 66 | + |
| 67 | +* file with the same name will be overwritten if exists in the target location |
0 commit comments