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:
onProgressChanged:(bytesCopied, totalBytes?)bytesCopied:Bereits heruntergeladene Byte‑AnzahltotalBytes:Gesamt‑Byte‑Größe der Datei, nullable Typ (long?)
Wenn der Server den Antwort‑HeaderContent‑Lengthnicht liefert, gilttotalBytes.HasValue == false. Ein Fortschrittsprozentsatz kann dann nicht berechnet werden, nur die heruntergeladenen Bytes lassen sich ausgeben.
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
| Funktion | API | Kern‑Methode |
|---|---|---|
| GET‑Anfrage | Http.Get(url) | Basis‑Abfrage, Datei‑Download .DownloadTo() |
| POST‑Formular | Http.Post(url) | .Form() Formular‑Schlüssel‑Wert‑Paare |
| POST JSON | Http.Post(url) | .Body(String,Medientyp) |
| Datei‑Upload | Http.Post(url) | .Upload() + NamedFileStream |
Datei‑Download DownloadTo
Previous: Datei‑Upload Upload