How to upload files using minimal APIs in ASP.NET Core
Briefly

Visual Studio 2022 can create an ASP.NET Core Web API project by selecting the ASP.NET Core Web API template, naming the project, and choosing its location. Configure the project to use .NET 9.0 and disable controllers to enable minimal APIs. Keep authentication set to None and leave Open API, HTTPS configuration, and Docker disabled if those features are not needed. Minimal APIs accept file uploads via IFormFile for single uploads and IFormFileCollection for multiple uploads. Example code saves an uploaded IFormFile to a temporary file using Path.GetTempFileName, File.OpenWrite, and CopyToAsync.
Launch the Visual Studio 2022 IDE. Click on "Create new project." In the "Create new project" window, select "ASP.NET Core Web API" from the list of templates displayed. Click Next. In the "Configure your new project" window, specify the name and location for the new project. Optionally check the "Place solution and project in the same directory" check box, depending on your preferences. Click Next.
In the "Additional Information" window shown next, select ".NET 9.0 (Standard Term Support)" as the framework version and uncheck the check box that says "Use controllers," as we'll be using minimal APIs in this project. Elsewhere in the "Additional Information" window, leave the "Authentication Type" set to "None" (the default) and make sure the check boxes "Enable Open API Support," "Configure for HTTPS," and "Enable Docker" remain unchecked. We won't be using any of those features here. Click Create.
IFormFile and IFormFileCollection in ASP.NET Core In the recent versions of ASP.NET Core, minimal APIs provide support for uploading files using the IFormFile and IFormFileCollection interfaces. While IFormFile is used to upload a single file, IFormFileCollection is used to upload multiple files. The following code snippet illustrates how you can upload a single file using IFormFile in your minimal API application. app.MapPost("/upload", async (IFormFile file) => { var tempFile = Path.GetTempFileName(); using var fileStream = File.OpenWrite(tempFile); await file.CopyToAsync(fileStream); }); Note that the File.OpenWrite method accepts the path to a file in your file system as a parameter and returns a FileStream instance. As its name indicates, a FileStream object provides a for a file, meaning a sequence of
Read at InfoWorld
[
|
]