JumpKick.HttpLib — File Download DownloadTo
Description
Use .DownloadTo(savePath, ...) to directly download and save network resources to local disk, usually used with Http.Get().
Callback parameters:
onProgressChanged:(bytesCopied, totalBytes?)bytesCopied: Number of bytes already downloadedtotalBytes: Total file bytes, nullable type (long?)
If the server does not return theContent‑Lengthresponse header,totalBytes.HasValue == false. Download percentage cannot be calculated, only downloaded bytes can be printed.
onSuccess:(headers)
Completion callback for download, you can retrieve response header information.
Examples
With progress and completion callback
using System;
using JumpKick.HttpLib;
class Program
{
static void Main(string[] args)
{
string url = "https://codelexample.test/file.zip";
string savePath = @"E:\httplib.zip";
Http.Get(url)
.DownloadTo(
savePath,
onProgressChanged: (bytesCopied, totalBytes) =>
{
if (totalBytes.HasValue)
{
// *1.0d force floating‑point arithmetic to avoid integer division yielding zero
double progress = (bytesCopied / totalBytes.Value * 1.0d) * 100;
Console.WriteLine($"Download progress:{progress:F2} %");
}
Console.WriteLine($"Downloaded bytes:{bytesCopied} bytes");
},
onSuccess: (headers) =>
{
Console.WriteLine("\nFile download completed");
}
)
.OnFail(ex =>
{
Console.WriteLine("Download error:" + ex.Message);
})
.Go();
Console.ReadKey();
}
}Code language: JavaScript (javascript)
Example‑Minimal Download
No progress monitoring
using System;
using JumpKick.HttpLib;
class Program
{
static void Main(string[] args)
{
Http.Get("https://httpbin.org/image/png")
.DownloadTo(@"E:\test.png",
onSuccess: headers =>
{
Console.WriteLine("Download finished");
})
.OnFail(ex => Console.WriteLine(ex.Message))
.Go();
Console.ReadKey();
}
}Code language: JavaScript (javascript)
Exception Test
Supply non‑existent resource address to catch 404 error
using System;
using JumpKick.HttpLib;
class Program
{
static void Main(string[] args)
{
// Invalid link triggers 404
Http.Get("https://httpbin.org/notfoundfile")
.DownloadTo(@"E:\error.png",
onProgressChanged: (bytesCopied, totalBytes) =>
{
Console.WriteLine($"Downloaded:{bytesCopied} bytes");
},
onSuccess: headers =>
{
Console.WriteLine("Download finished");
})
.OnFail(ex =>
{
Console.WriteLine("Caught exception:" + ex.Message);
})
.Go();
Console.ReadKey();
}
}Code language: JavaScript (javascript)
Summary
| Feature | API | Core Method |
|---|---|---|
| GET Request | Http.Get(url) | Basic query, file download .DownloadTo() |
| POST Form | Http.Post(url) | .Form() form key‑value pairs |
| POST JSON | Http.Post(url) | .Body(string, mediaType) |
| File Upload | Http.Post(url) | .Upload() + NamedFileStream |
File Download DownloadTo
Previous: File Upload