Uploading and Downloading Files
In this lesson, we will learn how to upload and download files.
Files selected with an input of type file can be uploaded in ViewModel properties of type IFormFile
. The IFormFile
type is an ASP.NET Core interface that was specifically defined for handling file uploads. If the property is defined as IEnumerable<IFormFile>
, and the input type file has the multiple
attribute, multiple simultaneous file uploads will be allowed.
File download is possible thanks to several types of IAsyncResult
implementations that allow bytes from various sources(files on disk, byte arrays, etc.) to be added to the HTTP response.
Handling file uploads in the view
First of all, only forms with enctype="multipart/form-data"
are able to upload files. In fact, forms declared this way produce a response with several sections, each with a possible different content type. Each file is placed in a separate section while all non-file fields are grouped in a single section.
The example below shows a typical form that allows file upload:
Get hands-on with 1200+ tech skills courses.