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
Previous: POST 請求
Next: 檔案下載 DownloadTo