IFormFile and the custom HelperPathProvider helper.
Overview
The file upload feature allows users to upload files to the server and stores them in designated folders (Images, Uploads, Temporal, or Facturas). The implementation handles file streaming and provides both physical paths and URL paths for uploaded files.Complete Implementation
Create the Controller
The Key Points:
UploadFilesController handles file uploads using dependency injection for the HelperPathProvider.Controllers/UploadFilesController.cs
- Uses
IFormFileparameter to receive uploaded files HelperPathProvidergenerates both physical and URL paths- Async file streaming with
CopyToAsyncfor better performance - Returns success message with file location
Create the View
The view provides a form with file input and displays the upload result.Important: The form must include
Views/UploadFiles/SubirFile.cshtml
enctype="multipart/form-data" to handle file uploads.Configure HelperPathProvider
Register the The helper provides methods to work with different folder types:See HelperPathProvider.cs:21 and HelperPathProvider.cs:46
HelperPathProvider as a singleton service in your application.Program.cs
Helpers/HelperPathProvider.cs
Usage Example
- Navigate to
/UploadFiles/SubirFile - Select a file using the file input
- Click “Subir fichero” to upload
- The file is saved to
wwwroot/images/directory - Success message displays the physical path
- URL path is available for accessing the uploaded file
Available Folder Options
| Folder | Physical Directory | Use Case |
|---|---|---|
Folders.Images | wwwroot/images/ | Image uploads |
Folders.Uploads | wwwroot/uploads/ | General file uploads |
Folders.Temporal | wwwroot/temp/ | Temporary files |
Folders.facturas | wwwroot/facturas/ | Invoice documents |
Related
HelperPathProvider
Complete API reference for path mapping utilities
Controllers
UploadFilesController API reference