I am developing an ASP.NET Core 8.0 Web API for my bachelor’s project. The API includes an upload endpoint for processing Excel files containing location data. However, when implementing the endpoint, Swagger fails to load and throws the following error:
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Error reading parameter(s) for action WebApi.Controllers.LocationsController.UploadFile (WebApi) as [FromForm] attribute used with IFormFile. Please refer to https://github.com/domaindrivendev/Swashbuckle.AspNetCore#handle-forms-and-file-uploads for more information
In Postman, when I send a file via multipart/form-data, I get the error:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"errors": { "file": ["The file field is required."] }
}
Controller Code:
[HttpPost("upload")]
[RequestSizeLimit(104857600)] // Set request size limit to 100 MB
public async Task<IActionResult> UploadFile([FromForm] IFormFile file)
{
var validationMessage = UploadHandler.ValidateFile(file);
if (!string.IsNullOrEmpty(validationMessage))
return BadRequest(validationMessage);
try
{
using var stream = new MemoryStream();
await file.CopyToAsync(stream);
using var package = new ExcelPackage(stream);
var worksheet = package.Workbook.Worksheets.FirstOrDefault();
if (worksheet == null)
return BadRequest("Invalid Excel file.");
var locations = new List<Location>();
for (int row = 2; row <= worksheet.Dimension.End.Row; row++) // Start from row 2 to skip headers
{
var location = new Location
{
WhId = worksheet.Cells[row, 1].Text, // Column A
LocationId = worksheet.Cells[row, 2].Text, // Column B
ShortLocationId = worksheet.Cells[row, 4].Text, // Column D
NmHallId = worksheet.Cells[row, 43].Text, // Column AP
NmAisle = worksheet.Cells[row, 46].Text // Column AS
};
locations.Add(location);
// Save data in batches of 1000 rows
if (locations.Count >= 1000)
{
_context.Locations.AddRange(locations);
await _context.SaveChangesAsync();
locations.Clear();
}
}
// Save remaining rows
if (locations.Any())
{
_context.Locations.AddRange(locations);
await _context.SaveChangesAsync();
}
return Ok(new { Message = "File uploaded successfully.", RowsProcessed = worksheet.Dimension.End.Row - 1 });
}
catch (Exception ex)
{
return BadRequest($"Error processing file: {ex.Message}");
}
}
Model Class:
The Location model is a complex object with multiple properties. However, I believe the issue is related to the [FromForm] usage with IFormFile.
What I've Tried:
- Validating the endpoint logic:
- Reduced the logic to minimal file presence validation.
- Confirmed that
fileis passed as[FromForm] IFormFile. - Still results in "file field is required" in Postman.
- Debugging Swagger:
- Commenting out the upload controller allows Swagger to load.
- The
[FromForm]parameter withIFormFileseems to cause Swagger crashes.
- Checked Postman:
- Verified
Content-Typeis automatically set tomultipart/form-data. - No manually added
Content-Typeheader.
Question:
How can I possibly resolve this issue? I have tried using ChatGPT, but it hasn't been very helpful in troubleshooting with me.
[FromForm]?[FromForm], the code works and swagger loads. I uploaded the file and gave a success response. Thank you so much! @YongShun