Téléchargement de fichiers DownloadTo

JumpKick.HttpLib — Téléchargement de fichiers DownloadTo

Description

Utilisez .DownloadTo(cheminDeSauvegarde, ...) pour enregistrer directement des ressources distantes sur le disque local, généralement associé à Http.Get().
Paramètres de rappel :

  1. onProgressChanged:(bytesCopied, totalBytes?)
    • bytesCopied : Nombre d’octets déjà téléchargés
    • totalBytes : Nombre total d’octets du fichier, type nullable (long?)
      Si le serveur ne renvoie pas l’en‑tête Content‑Length, totalBytes.HasValue == false. Le pourcentage de progression ne peut pas être calculé, seuls les octets téléchargés peuvent être affichés.
  2. onSuccess:(headers)
    Rappel de fin de téléchargement, permet de récupérer les en‑têtes de réponse.

Exemples

Avec progression et rappel de fin

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 le calcul à virgule flottante pour éviter une division entière égale à zéro
                    double progress = (bytesCopied / totalBytes.Value * 1.0d) * 100;
                    Console.WriteLine($"Progression téléchargement:{progress:F2} %");
                }
                Console.WriteLine($"Octets téléchargés:{bytesCopied} bytes");
            },
            onSuccess: (headers) =>
            {
                Console.WriteLine("\nTéléchargement terminé");
            }
        )
        .OnFail(ex =>
        {
            Console.WriteLine("Erreur téléchargement:" + ex.Message);
        })
        .Go();

        Console.ReadKey();
    }
}Langage du code : JavaScript (javascript)

Exemple‑Téléchargement minimal

Sans suivi de progression

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("Téléchargement terminé");
            })
        .OnFail(ex => Console.WriteLine(ex.Message))
        .Go();

        Console.ReadKey();
    }
}Langage du code : JavaScript (javascript)

Test d’exception

Indiquez une ressource inexistante pour intercepter une erreur 404

using System;
using JumpKick.HttpLib;

class Program
{
    static void Main(string[] args)
    {
        // Lien invalide, déclenche 404
        Http.Get("https://httpbin.org/notfoundfile")
        .DownloadTo(@"E:\error.png",
            onProgressChanged: (bytesCopied, totalBytes) =>
            {
                Console.WriteLine($"Téléchargé:{bytesCopied} bytes");
            },
            onSuccess: headers =>
            {
                Console.WriteLine("Téléchargement terminé");
            })
        .OnFail(ex =>
        {
            Console.WriteLine("Exception interceptée:" + ex.Message);
        })
        .Go();

        Console.ReadKey();
    }
}Langage du code : JavaScript (javascript)

Synthèse

FonctionnalitéAPIMéthode principale
Requête GETHttp.Get(url)Requête basique, téléchargement fichier .DownloadTo()
POST FormulaireHttp.Post(url).Form() paires clé‑valeur formulaire
POST JSONHttp.Post(url).Body(chaîne,typeMédia)
Téléversement fichierHttp.Post(url).Upload() + NamedFileStream

Téléchargement de fichiers DownloadTo

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *