파일 업로드 Upload

JumpKick.HttpLib — 파일 업로드 Upload

설명

파일 업로드는 Http.Post() + .Upload() 를 사용하며 multipart/form‑data 형식으로 로컬 파일을 전송합니다.
핵심 클래스:NamedFileStream(폼 필드 이름, 파일 이름, MIME 타입, 파일 스트림)

공식 예제 주소 https://jthorne.co.uk/httplib 는 실제 테스트할 수 없습니다. 테스트용 대체 주소:https://httpbin.org/post

생성자

new NamedFileStream(
    string fieldName,       // 백엔드가 수신하는 폼 키
    string fileName,        // 서버에 전달할 파일명
    string contentType,     // 파일 MIME 타입
    Stream stream           // 파일 읽기 스트림 File.OpenRead(파일 경로)
)Code language: JavaScript (javascript)

단일 파일 업로드

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("==== 파일 업로드 성공 ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== 업로드 오류 ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

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

업로드 진행률 감지 onProgressChanged

업로드 퍼센트를 실시간으로 출력

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($"업로드 중:{percent:F2} %");
            }
        )
        .OnSuccess(result =>
        {
            Console.WriteLine("\n==== 파일 업로드 완료 ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== 업로드 오류 ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

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

여러 파일 동시 업로드

배열 안에 NamedFileStream 인스턴스를 여러 개 추가합니다

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("==== 다중 파일 업로드 성공 ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== 업로드 오류 ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

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

오류 발생 케이스

존재하지 않는 파일 경로로 예외를 발생시켜 OnFail 가 정상 동작하는지 확인

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:\존재하지않는파일.png") // 잘못된 파일 경로
            )
        })
        .OnSuccess(result =>
        {
            Console.WriteLine("==== 파일 업로드 성공 ====");
            Console.WriteLine(result);
        })
        .OnFail(webexception =>
        {
            Console.WriteLine("==== 예외 포착(파일 없음) ====");
            Console.WriteLine(webexception.Message);
        })
        .Go();

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

파일 업로드 Upload

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다