Upload/Download Files Using HttpClient in C#
Mục lục bài viết
Upload/Download Files Using HttpClient in C#
How to implement file uploading/downloading on the server side and on the client side.
In this short blog post, we will take a look at how to send multipart MIME data to a Web API using HttpClient. We will create two applications to demonstrate the data transfer between the client side and the server side. The server side app is an ASP.NET Core web project, which includes a Web API controller for uploading and downloading files. The client side app is a Console project, which contains a Typed HttpClient to send HTTP requests for file uploading and/or downloading.
When an application needs to talk to another system, it is quite common that the application sends data to and receives data from the other system using HttpClient
in the back-end. Based on this article in Microsoft Docs, it is straightforward to send HTTP requests and receive HTTP responses. However, most of tutorials and blog posts don’t talk much about sending FormData with a file object and a collection of key/value pairs using HttpClient. This blog post intends to provide the missing guide.
The complete source code is in my GitHub repository. Now, let’s review some code.
Web API for Uploading a File with FormData
This API action method follows the example in an article in Microsoft Docs. The implementation is lengthy, but the code logic demonstrates several checks to meet the security criteria. The following code snippet shows the example action method.
gist link
In the code above, lines 22 to 47 handle the File Disposition, and lines 48 to 65 handle the Form Disposition. When the MultipartReader
reads sections in the HTTP request body, the request content is parsed and saved to physical disk (file) and memory (form data).
Web API for Downloading a File
This API action method finds the file and converts the file to an array of bytes, then returns a FileContentResult
with the byte array and metadata. The following code snippet shows an example.
gist link
With the server side app ready, let’s take a look at the client side app.
Send Multipart FormData using HttpClient
We need to use an HTTP Post method to send content to a server side resource. The tricky part is constructing the HTTP request body content because we need to combine the file data and a collection of key/value pairs in one FormData object. The following code snippet shows an example solution.
gist link
The method UploadFile(string filePath)
first validates the physical file. Then line 13 instantiates a MultipartFormDataContent
object, which is the request content sent to the server side app.
Lines 14 and 15 create a ByteArrayContent
object from the file content, and sets the ContentType
header to be “multipart/form-data”
. Note: When a file is included in a form, the enctype
attribute should always be “multipart/form-data”
, which specifies that the form will be sent as a multipart MIME message. If the ContentType
is not set, then it will default to be application/json
, which is not what we want here.
Line 16 adds the file content to the form object, and sets the key to be “file”
. The key can be different when multiple files are included in a form.
Lines 17 to 19 are examples of adding key/value pairs to the MultipartFormDataContent
object. The values can only be represented as strings, and the server side app will have to parse them into correct data types.
Line 21 sends the HTTP post request when the request content is ready. Lines 23 and 24 receive the HTTP response for later use.
Download a File using HttpClient
In order to download a file, we make an HTTP Get request, then read the response content into a memory stream which can be copied to a physical file. The following code snippet shows an example.
gist link