File Upload Tutorial
Uploading a file in an ASP.NET Zero application is not different than a regular ASP.NET Core application. In this tutorial, we will implement a file upload functionality in ASP.NET Zero. We will also send an additional field to server while uploading a file.
This tutorial assumes that, you already have a valid ASP.NET Zero license and already created an empty project by following Getting Started document.
You can implement two different way of file upload to MVC projects. Ajax based implementation and form based implementation.
Form Based Implementation
- First, create a class named FileUploadViewModel in *.Web.Mvc\Areas\AppAreaName\Models folder. This class will be used to transfer additional parameters during the upload process.
- Then, create a controller named FileUploadController in *.Web.Mvc\Areas\AppAreaName\Controllers folder.
- Create a cshtml file named Index.cshtml in *.Web.Mvc\Areas\AppAreaName\Views\FileUpload folder.
Go to *.Web.Mvc\Areas\AppAreaName\Startup\AppAreaNameNavigationProvider.cs and add a new menu item.
Then you will have a file upload page as seen below.
After you fill the description area, select a file and click to upload, you will see that it refreshes the page. Not to refresh page you can use ajax to upload file.
Ajax Based Implementation
- First, create a class named FileUploadViewModel in *.Web.Mvc\Areas\AppAreaName\Models folder. This class will be used to transfer additional parameters during the upload process.
- Then, create a controller named FileUploadController in *.Web.Mvc\Areas\AppAreaName\Controllers folder.
- Create a cshtml file named Index.cshtml in *.Web.Mvc\Areas\AppAreaName\Views\FileUpload folder.
- Then you will have same file upload page.
After you fill the description area, select a file and click to upload, you will see that it does not the page. With ajax based file upload you can upload file without refreshing page.