Upload/Download file(s) in ASP.NET CORE

.Net Core 3.1

3 min readJan 16, 2020
Photo from: https://www.dreamstime.com/royalty-free-stock-images-cloud-computing-upload-download-icons-image20770929

Create web APIs for providing file(s) uploading and downloading across client and server would be normal things. The article will introduce how to implement through ASP.NET CORE.

Beginning with create empty web API project in visual studio and target framework choose .Net Core 3.1.

Create controller called FileController that provide interface for manipulate file(s).

Create service layer to implement detail for transferring file(s).

Everything would be ready now, let’s launch our web API application and complete the testing step via fill up some requests inside the Postman.

Testing download interface, first we created our root directory: “D:\webroot\” and sub-directory: “report”, then put some files inside.

Files under folder: D:\webroot\report\

Initial a request under Postman:

Testing download interface
1) Naming request name.
2) Fill the correspond URL using GET method.
3) Fire the request and checking response.

Testing upload interface, initial a request on Postman:

Testing upload interface
1) Naming request name.
2) Fill the correspond URL using POST method.
Over <Body> Tag: choose form-data
i. KEY: files (type select File), VALUE: choose any file you want.
ii. KEY: subDirectory, VALUE: target folder.
✨You could upload single file or multiple files as you wish.
✨The KEY's property name mapping to WEB API parameter's name.
3) Fire the request and checking response.

Something you need to notice if you would like to porting your web API from .Net Core 2.2 to 3.1

The below sample works under web API .Net Core 2.2 if your client’s data-form(web page) not given specific name.

For example:
<form method=”post” enctype=”multipart/form-data” action=”/api/upload”>
<input type=”file” name=”” multiple />
<br />
<input type=”submit” value=”submit” />

[HttpPost(“upload”)]public async Task<IActionResult> UploadFile([FromForm]List<IFormFile> files){…}

But for Net Core 3.1 you will get failed to bind your model IFormFile from the request (that means list: files.Count always return zero):

Binding failed

The correct way would be:

[HttpPost("upload")]public IActionResult UploadFile([FromForm(Name = ""] List<IFormFile> files, string subDirectory){...}





Coding for fun. (Either you are running for food or running for being food.)