6.24Uploading file resources
In internet applications, users uploading files is a common requirement, covering scenarios such as setting avatars, publishing image-and-text posts, uploading albums to cloud drives, and sharing Vlog videos to video communities. The following shows several ways to implement file uploads.
Uploading Using the Form Form Method
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 keep adding them to multipart (keeping the form name consistent, e.g. files):
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, the builder pattern is also supported, along with retrieving the return value of the upload. For more details, refer to Section 2.1.
// 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.1However, this approach to uploading file resources is not flexible enough when facing various complex scenarios — for example, it cannot track upload progress in real time, restrict upload file type and size, or implement resumable uploads. In addition, it may require developers to write more extra code. Therefore, the framework integrates features specifically designed for uploading file resources to address these issues.
Uploading Using the Framework's Built-in Dedicated Upload Feature (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 progress in real time and allows restricting file type and size.
The following example shows how to print the upload progress:
await httpRemoteService.UploadFileAsync("https://localhost:7044/HttpRemote/AddFile", @"C:\Workspaces\httptest.jpg", "file" , async progress => { Console.WriteLine(await progress.ToSummaryStringAsync()); // Print brief progress information });Console output example:
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 file upload progress in the console in real time, it is recommended to use the UpdateConsoleProgressAsync() method. An example follows:
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 will display the following progress information:
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 file type and size, do the following:
await httpRemoteService.SendAsync(HttpRequestBuilder.UploadFile("https://localhost:7044/HttpRemote/AddFile", @"C:\Workspaces\httptest.jpg", "file" , async progress => { Console.WriteLine(await progress.ToSummaryStringAsync()); // Print brief progress information }) .SetAllowedFileExtensions(".jpg;.png") // Allow only jpg and png types .SetMaxFileSizeInBytes(5 * 1024 * 1024)); // Restrict file size to 5MBIf you need to append additional form parameters when uploading a file, do the following:
await httpRemoteService.SendAsync(HttpRequestBuilder.UploadFile("https://localhost:7044/HttpRemote/AddFile", @"C:\Workspaces\httptest.jpg", "file" , async progress => { Console.WriteLine(await progress.ToSummaryStringAsync()); // Print brief progress information }) .WithMultipart(multipart => { multipart.AddText("Furion", "name"); });With the above approaches, you can flexibly meet various file upload requirements.