File Download DownloadTo

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:

  1. onProgressChanged:(bytesCopied, totalBytes?)
    • bytesCopied: Number of bytes already downloaded
    • totalBytes: Total file bytes, nullable type (long?)
      If the server does not return the Content‑Length response header, totalBytes.HasValue == false. Download percentage cannot be calculated, only downloaded bytes can be printed.
  2. 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

FeatureAPICore Method
GET RequestHttp.Get(url)Basic query, file download .DownloadTo()
POST FormHttp.Post(url).Form() form key‑value pairs
POST JSONHttp.Post(url).Body(string, mediaType)
File UploadHttp.Post(url).Upload() + NamedFileStream

File Download DownloadTo

Previous:

Leave a Reply

Your email address will not be published. Required fields are marked *