Can't import/upload .XLSX/.XLS files in Swagger (ASP NET Core)
Hello guys, it's my first post here and I'm glad to be part of this community. So, I've an ASP NET Core Web API that uses Swagger, and one of the methods must import an IFormFile as an Excel (.xlsx or .xls). I've already added the corresponding MIME Types on my FileUploadOperation.cs but when I hit the Swagger button do execute the action after choose a .xlsx file, the field becomes red and I can't import the file. The code above is my FileUploadOperation.cs: public class FileUploadOperation : IOperationFilter { private const string formDataMimeType = "multipart/form-data"; private const string formDataMimeType2 = "application/vnd.ms-excel"; private const string formDataMimeType3 = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; private static readonly string[] formFilePropertyNames = typeof(IFormFile).GetTypeInfo().DeclaredProperties.Select(p => p.Name).ToArray(); public void Apply(Operation operation, OperationFilterContext context) { var parameters = operation.Parameters; if (parameters == null || parameters.Count == 0) return; var formFileParameterNames = new List<string>(); var formFileSubParameterNames = new List<string>(); foreach (var actionParameter in context.ApiDescription.ActionDescriptor.Parameters) { var properties = actionParameter.ParameterType.GetProperties() .Where(p => p.PropertyType == typeof(IFormFile)) .Select(p => p.Name) .ToArray(); if (properties.Length != 0) { formFileParameterNames.AddRange(properties); formFileSubParameterNames.AddRange(properties); continue; } if (actionParameter.ParameterType != typeof(IFormFile)) continue; formFileParameterNames.Add(actionParameter.Name); } if (!formFileParameterNames.Any()) return; var consumes = operation.Consumes; consumes.Clear(); consumes.Add(formDataMimeType); if (operation.OperationId.Equals("PostProcessBatch")) { consumes.Add(formDataMimeType2); consumes.Add(formDataMimeType3); } foreach (var parameter in parameters.ToArray()) { if (!(parameter is NonBodyParameter) || parameter.In != "formData") continue; if (formFileSubParameterNames.Any(p => parameter.Name.StartsWith(p + ".")) || formFilePropertyNames.Contains(parameter.Name)) parameters.Remove(parameter); } foreach (var formFileParameter in formFileParameterNames) { parameters.Add(new NonBodyParameter() { Name = formFileParameter, Type = "file", In = "formData" }); } } } Can anyone help me with the solution?1.1KViews0likes0Comments