codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

File Upload via Swagger

How to upload a single file, upload a list of files, and upload a file in a FormData object.

Changhui Xu
codeburst
Published in
5 min readApr 3, 2020

--

Many of us might have read the article “How to upload file via Swagger in ASP.NET Core Web API” by Talking Dotnet (link, 2017) on this topic. I personally didn’t bother about working with file-uploading in Swagger, because it was not so straightforward and it needed some cumbersome work. It has been a long time since that article was published, so it might be worth taking another look at the library Swashbuckle.AspNetCore.

Thanks to the author and contributors, Swashbuckle is always actively updating and improving. They made several major improvements with the release of version 5 in January 2020. Swashbuckle.AspNetCore v5 now supports Swagger/OpenAPI v3 and ASP.NET Core 3. The new Swashbuckle also honors the System.Text.Json serializer instead of Newtonsoft by default.

In this article, we will go over examples about uploading a single file, uploading a list of files, and uploading a file in a FormData object. The complete source code is in this GitHub repository, and you can view the demo website here.

Upload a Single File

In this case, we only need to set up an API endpoint which takes an IFormFile object. The following code snippet shows an example.

With the new release of Swashbuckle.AspNetCore library, we don’t need to create a custom OperationFilter for the file upload operation. Swashbuckle automatically supports the IFormFile data type in the user interface, as shown in the following screenshot.

We can drag and drop a file to the file input field, and the request will be in the multipart/form-data media type.

Upload a List of Files

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Changhui Xu

Lead Application Developer. MBA. I write blogs about .NET, Angular, JavaScript/TypeScript, Docker, AWS, DDD, and many others.

Responses (2)

Write a response