Datei‑Download DownloadTo

JumpKick.HttpLib — Datei‑Download DownloadTo

Hinweis

Verwenden Sie .DownloadTo(Speicherpfad, ...) um Netzwerk‑Ressourcen direkt auf die lokale Festplatte herunterzuladen. Wird üblicherweise zusammen mit Http.Get() benutzt.
Rückruf‑Parameter:

  1. onProgressChanged:(bytesCopied, totalBytes?)
    • bytesCopied:Bereits heruntergeladene Byte‑Anzahl
    • totalBytes:Gesamt‑Byte‑Größe der Datei, nullable Typ (long?)
      Wenn der Server den Antwort‑Header Content‑Length nicht liefert, gilt totalBytes.HasValue == false. Ein Fortschrittsprozentsatz kann dann nicht berechnet werden, nur die heruntergeladenen Bytes lassen sich ausgeben.
  2. onSuccess:(headers)
    Rückruf bei abgeschlossenem Download, Antwort‑Header‑Daten können abgerufen werden.

Beispiele

Mit Fortschritts‑ und Abschluss‑Rückruf

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 erzwingt Gleitkomma‑Berechnung, damit Ganzzahldivision nicht Null ergibt
                    double progress = (bytesCopied / totalBytes.Value * 1.0d) * 100;
                    Console.WriteLine($"Download‑Fortschritt:{progress:F2} %");
                }
                Console.WriteLine($"Heruntergeladene Bytes:{bytesCopied} bytes");
            },
            onSuccess: (headers) =>
            {
                Console.WriteLine("\nDatei‑Download abgeschlossen");
            }
        )
        .OnFail(ex =>
        {
            Console.WriteLine("Download‑Fehler:" + ex.Message);
        })
        .Go();

        Console.ReadKey();
    }
}Code-Sprache: JavaScript (javascript)

Beispiel‑Minimal‑Download

Ohne Fortschritts‑Überwachung

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 abgeschlossen");
            })
        .OnFail(ex => Console.WriteLine(ex.Message))
        .Go();

        Console.ReadKey();
    }
}Code-Sprache: JavaScript (javascript)

Ausnahme‑Test

Nicht vorhandene Ressourcen‑Adresse angeben, um 404‑Fehler abzufangen

using System;
using JumpKick.HttpLib;

class Program
{
    static void Main(string[] args)
    {
        // Ungültige URL, löst 404 aus
        Http.Get("https://httpbin.org/notfoundfile")
        .DownloadTo(@"E:\error.png",
            onProgressChanged: (bytesCopied, totalBytes) =>
            {
                Console.WriteLine($"Heruntergeladen:{bytesCopied} bytes");
            },
            onSuccess: headers =>
            {
                Console.WriteLine("Download abgeschlossen");
            })
        .OnFail(ex =>
        {
            Console.WriteLine("Ausnahme abgefangen:" + ex.Message);
        })
        .Go();

        Console.ReadKey();
    }
}Code-Sprache: JavaScript (javascript)

Übersicht

FunktionAPIKern‑Methode
GET‑AnfrageHttp.Get(url)Basis‑Abfrage, Datei‑Download .DownloadTo()
POST‑FormularHttp.Post(url).Form() Formular‑Schlüssel‑Wert‑Paare
POST JSONHttp.Post(url).Body(String,Medientyp)
Datei‑UploadHttp.Post(url).Upload() + NamedFileStream

Datei‑Download DownloadTo

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert