File Upload

JumpKick.HttpLib — File Upload

Overview

File upload is implemented with Http.Post() + .Upload(), using the multipart/form‑data format for local file uploads;
Core class: NamedFileStream(form‑field name, file name, MIME type, file stream)

The official demo endpoint https://jthorne.co.uk/httplib cannot be used for real‑world testing. Use this replacement test endpoint: https://httpbin.org/post

Constructor

new NamedFileStream(
    string fieldName,       // Form key the backend receives for the file
    string fileName,        // Filename sent to the server
    string contentType,     // File MIME type
    Stream stream           // File read stream File.OpenRead(file path)
)Code language: JavaScript (javascript)

Single‑File 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("==== File upload succeeded ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== Upload error ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

        Console.ReadKey();
    }
}Code language: JavaScript (javascript)

Upload Progress Listener onProgressChanged

Print upload percentage in real‑time

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($"Uploading: {percent:F2} %");
            }
        )
        .OnSuccess(result =>
        {
            Console.WriteLine("\n==== File upload completed ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== Upload error ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

        Console.ReadKey();
    }
}Code language: JavaScript (javascript)

Multiple‑File Upload

Append multiple NamedFileStream objects inside the array

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("==== Multi‑file upload succeeded ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== Upload error ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

        Console.ReadKey();
    }
}Code language: JavaScript (javascript)

Error Scenario

Supply a non‑existent file path to trigger an exception and verify OnFail handling

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:\nonexistent.png") // Wrong file path
            )
        })
        .OnSuccess(result =>
        {
            Console.WriteLine("==== File upload succeeded ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== Exception caught (file not found) ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

        Console.ReadKey();
    }
}Code language: JavaScript (javascript)

File Upload

Leave a Reply

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