Menu

HowTo: upload files with Aurelia to ASP.NET Core (API) controller

04 January 2017

Within ASP.NET Core file uploads are somewhat different than we used to. In previous versions ASP.NET uses HttpPostedFileBase to bind files server-side. This concept is replaced by IFormFile in ASP.NET Core.

The basics of uploading a file with ASP.NET MVC is very well explained here goed uitgelegd. To upload files with Razor and ASP.NET Core take a look at the file upload docs from Microsoft.

Because nowadays there is much use of JavaScript client frameworks such as Angular and Aurelia I'll focus on how to upload a file from a Aurelia web application to MVC 6 (api) controller.

Aurelia is a JavaScript client framework uses the fetch-api to request the backend.

To begin, we'll show you the HTML so that the user can select a file in his browser:

Striking is the abscence of the method-parameter. The default (synchronous) way as described the ASP.NET Core docs uses the method-parameter to make the file available in the controller through modelbinding. In our SPA scenario my expierence is that this method-parameter always have a null value. Therefore I've chosen to asynchronous path with await Request.ReadFormAsync().

That's it! This is how you could upload a file (or more) from a Aurelia web application to an ASP.NET Core (API) controller.