Datei‑Upload Upload

JumpKick.HttpLib — Datei‑Upload Upload

Beschreibung

Der Datei‑Upload basiert auf Http.Post() + .Upload() und sendet lokale Dateien im multipart/form‑data‑Format;
Kernklasse:NamedFileStream(Formularfeld‑Name, Dateiname, MIME‑Typ, Dateistream)

Die offizielle Beispiel‑URL https://jthorne.co.uk/httplib eignet sich nicht für echte Tests. Nutzen Sie diese Ersatz‑Adresse:https://httpbin.org/post

Konstruktor

new NamedFileStream(
    string fieldName,       // Formular‑Schlüssel für den Empfang auf Serverseite
    string fileName,        // An Server übergebener Dateiname
    string contentType,     // MIME‑Typ der Datei
    Stream stream           // Lese‑Stream der Datei File.OpenRead(Dateipfad)
)Code-Sprache: PHP (php)

Einzel‑Datei‑Upload

using System;
using System.IO;
using JumpKick.HttpLib;

class Program
{
    static void Main(string[] args)
    {
        Http.Post("https://httpbin.org/post")
        .Upload(files: new[]
        {
            new NamedFileStream(
                "myfile",
                "photo.png",
                "image/png",
                File.OpenRead(@"C:\Users\Jack\Pictures\clipboard.png")
            )
        })
        .OnSuccess(result =>
        {
            Console.WriteLine("==== Datei‑Upload erfolgreich ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== Upload‑Fehler ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

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

Fortschritts‑Callback onProgressChanged

Gibt den Upload‑Prozentsatz in Echtzeit aus

using System;
using System.IO;
using JumpKick.HttpLib;

class Program
{
    static void Main(string[] args)
    {
        Http.Post("https://httpbin.org/post")
        .Upload(
            files: new[]
            {
                new NamedFileStream(
                    "myfile",
                    "photo.png",
                    "image/png",
                    File.OpenRead(@"C:\Users\86186\Pictures\clipboard.png")
                )
            },
            onProgressChanged: (bytesSent, totalBytes) =>
            {
                double percent = (double)bytesSent / totalBytes * 100;
                Console.WriteLine($"Hochladen: {percent:F2} %");
            }
        )
        .OnSuccess(result =>
        {
            Console.WriteLine("\n==== Upload abgeschlossen ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== Upload‑Fehler ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

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

Mehrere Dateien gleichzeitig hochladen

Fügen Sie mehrere NamedFileStream‑Objekte dem Array hinzu

using System;
using System.IO;
using JumpKick.HttpLib;

class Program
{
    static void Main(string[] args)
    {
        Http.Post("https://httpbin.org/post")
        .Upload(files: new[]
        {
            new NamedFileStream("file1", "1.png", "image/png", File.OpenRead(@"D:\1.png")),
            new NamedFileStream("file2", "2.jpg", "image/jpeg", File.OpenRead(@"D:\2.jpg"))
        })
        .OnSuccess(result =>
        {
            Console.WriteLine("==== Mehrfach‑Upload erfolgreich ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== Upload‑Fehler ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

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

Fehlerfall

Verwenden Sie einen nicht vorhandenen Dateipfad um eine Ausnahme auszulösen und OnFail‑Behandlung zu prüfen

using System;
using System.IO;
using JumpKick.HttpLib;

class Program
{
    static void Main(string[] args)
    {
        Http.Post("https://httpbin.org/post")
        .Upload(files: new[]
        {
            new NamedFileStream(
                "myfile",
                "notfound.png",
                "image/png",
                File.OpenRead(@"D:\nicht_existente_datei.png") // falscher Pfad
            )
        })
        .OnSuccess(result =>
        {
            Console.WriteLine("==== Datei‑Upload erfolgreich ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== Ausnahme abgefangen (Datei nicht gefunden) ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

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

Datei‑Upload Upload

Schreibe einen Kommentar

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