2.5Uploading File Resources (OSS)

Created on Aug 17, 2026~7 min read

In internet applications, uploading files is a common requirement, covering scenarios such as setting an avatar, posting image-and-text updates, uploading albums to a cloud drive, and sharing a Vlog to a video community. The following demonstrates several ways to implement file uploads.

1. Uploading via the Form Form Method

Uploading files via the Form form method is consistent with the Form form submission method described in Section 2.3.

cs
await httpRemoteService.PostAsync("https://localhost:7044/HttpRemote/AddFile", builder => builder    .SetMultipartContent(multipart => multipart        .AddFileAsStream(@"C:\Workspaces\httptest.jpg", "file")));

If you need to upload multiple files, simply continue adding them in multipart (keeping the form name consistent, such as files):

cs
await httpRemoteService.PostAsync("https://localhost:7044/HttpRemote/AddFiles", builder => builder    .SetMultipartContent(multipart => multipart        .AddFileAsStream(@"C:\Workspaces\httptest.jpg", "files")        .AddFileFromRemote("https://furion.net/img/furionlogo.png", "files")));

In addition, it also supports using the builder pattern, as well as retrieving the return value of the file upload. For more details, refer to Section 2.1.

cs
// Use the builder patternawait httpRemoteService.SendAsync(HttpRequestBuilder.Post("https://localhost:7044/HttpRemote/AddFile")    .SetMultipartContent(multipart => multipart        .AddFileAsStream(@"C:\Workspaces\httptest.jpg", "file")));// For more detailed usage, refer to Section 2.1

2. Uploading via a Non-Form Form Method (OSS)

When integrating with certain OSS (object storage services) or cloud drives, you often encounter cases where the traditional Form form upload method is not supported. In such cases, you need to upload the file directly as a file byte array or a Stream. The following is a concrete implementation example:

cs
var fileStream = File.OpenRead("file path");   // Or use: var fileBytes = File.ReadAllBytes("file path");await httpRemoteService.PutAsync("https://localhost:7044/HttpRemote/AddFile", builder => builder    .SetContent(fileStream));   // Or use .SetContent(fileBytes);

In some special scenarios, you may need to explicitly remove the Content-Type request header (i.e., set it to empty). In this case, you can do so by calling the SetOmitContentType(true) method, as shown below:

cs
await httpRemoteService.PutAsync("https://localhost:7044/HttpRemote/AddFile", builder => builder    .SetContent(fileStream) // Or use .SetContent(fileBytes);    .SetOmitContentType(true);    // .AutoSetHostHeader());   // Some servers may enforce validation of the Host request header (optional)

3. Using the UploadFile Extension Method (Form Method)

In applications such as video sharing, users typically need to view real-time progress when uploading files. For this purpose, you can use the UploadFile extension method, which supports retrieving real-time progress and allows restricting the file type and size.

The following example shows how to print the upload progress:

cs
await httpRemoteService.UploadFileAsync("https://localhost:7044/HttpRemote/AddFile", @"C:\Workspaces\httptest.jpg", "file"    , async progress =>    {        Console.WriteLine(await progress.ToSummaryStringAsync());  // Output brief progress information    });

Console output example:

bash
Transferred 0.01MB of 0.01MB (100.00% complete, Speed: 0.86MB/s, Time: 0.01s, ETA: 0.00s), File: httptest.jpg, Path: C:\Workspaces\httptest.jpg.

If you need to display the file upload progress in the console in real time, it is recommended to use the UpdateConsoleProgressAsync() method. The example is as follows:

cs
await httpRemoteService.UploadFileAsync("https://localhost:7044/HttpRemote/AddFile", @"C:\Workspaces\httptest.jpg", "file"    , progress => progress.UpdateConsoleProgressAsync()); // Update the file transfer progress bar in the console// ✅ Or use the UploadFileWithConsoleProgressAsync method (with console progress printing)await httpRemoteService.UploadFileWithConsoleProgressAsync("https://localhost:7044/HttpRemote/AddFile", @"C:\Workspaces\httptest.jpg", "file");

After execution, the console displays the following progress information:

bash
File: httptest.jpg, Path: C:\Workspaces\httptest.jpg.[##################################################] 61.35% (0.01MB/0.01MB) Speed: 0.86MB/s, Time: 0.01s, ETA: 0.00s.

If you need to restrict the file type and size, do as follows:

cs
await httpRemoteService.SendAsync(HttpRequestBuilder.UploadFile("https://localhost:7044/HttpRemote/AddFile", @"C:\Workspaces\httptest.jpg", "file"    , async progress =>    {        Console.WriteLine(await progress.ToSummaryStringAsync());  // Output brief progress information    })    .SetAllowedFileExtensions(".jpg;.png")  // Only allow jpg and png types    .SetMaxFileSizeInBytes(5 * 1024 * 1024));  // Limit the file size to 5MB

If you need to attach additional form parameters when uploading a file, do as follows:

cs
await httpRemoteService.SendAsync(HttpRequestBuilder.UploadFile("https://localhost:7044/HttpRemote/AddFile", @"C:\Workspaces\httptest.jpg", "file"    , async progress =>    {        Console.WriteLine(await progress.ToSummaryStringAsync());  // Output brief progress information    })    .WithMultipart(multipart =>    {        multipart.AddText("Furion", "name");    });

Through the above approaches, you can flexibly meet various file upload requirements.