首页 > 解决方案 > Swagger 文件上传给出 415 Unsupported Media Type

问题描述

在创建 CRUD Rest API 时,我需要创建一种方法来导入代表一些 MongoDB 对象的文本文件。我正在使用 Swagger 3(对 OpenAPi 进行了更改)来创建一个基本的 UI 来测试这些服务。

在我的控制器上,我有以下 Import 方法定义:

        // POST: api/Campaign/Import
        //
        [HttpPost("/Import", Name = "Import")]
        public async Task<IActionResult> Import([FromBody] IFormFile campaignFile)
        {
            string resultFilePath = Path.Combine(folderPath, campaignFile.FileName);
            using (var fileContentStream = new MemoryStream())
            {
                await campaignFile.CopyToAsync(fileContentStream);
                await System.IO.File.WriteAllBytesAsync(resultFilePath, fileContentStream.ToArray());
            }
            var campaigns = await _campaignService.Import(resultFilePath);
            return CreatedAtRoute("Get", campaigns);
        }

想法是我从 POST 请求中接收表单文件信息,将其上传到服务器然后处理它(_campaignService.Import

我的导入操作的 Swagger 操作过滤器是:


 public class SwaggerFileOperationFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.OperationId == "Import")
            {
                operation.Parameters.Clear();

                var uploadFileMediaType = new OpenApiMediaType()
                {
                    Schema = new OpenApiSchema()
                    {
                        Type = "object", // the type of thing to send 
                        Properties =
                    {
                        ["campaignFile"] = new OpenApiSchema()
                        {
                            Description = "Upload File",
                            Type = "file",
                            Format = "binary",
                        }
                    },
                        Required = new HashSet<string>()
                    {
                        "campaignFile"
                    }
                    }
                };
                operation.RequestBody = new OpenApiRequestBody
                {
                    Content =
                    {
                        ["multipart/form-data"] = uploadFileMediaType
                    }
                };


            }
        }
    }

我发送的文件示例:


{ "_id" : ObjectId("5ebd813bcaf1800001be1e70"), "Name" : "Test Online 2", "Strategies" : [{ "_t" : ["StrategyType", "OnlineStrategy"], "StrategyName" : "OnlineGoogle", "StrategyType" : "Online", "StrategyBudget" : 1000, "ExtraElements" : { "URL" : "http:\\www.google.com", "Latitude" : 0.0, "Longitude" : 0.0 }, "URL" : "http:\\www.google.com" }, { "_t" : ["StrategyType", "TvStrategy"], "StrategyName" : "TVPortugal", "StrategyType" : "TV", "StrategyBudget" : 1000, "ExtraElements" : { "ChannelSlots" : [{ "ChannelName" : "SIC", "FromHour" : "13:30:00", "ToHour" : "13:30:00" }, { "ChannelName" : "TVI", "FromHour" : "15:30:00", "ToHour" : "16:30:00" }], "Latitude" : 0.0, "Longitude" : 0.0 }, "ChannelSlots" : [{ "ChannelName" : "SIC", "FromHour" : "13:30:00", "ToHour" : "13:30:00" }, { "ChannelName" : "TVI", "FromHour" : "15:30:00", "ToHour" : "16:30:00" }] }, { "_t" : ["StrategyType", "OutdoorStrategy"], "StrategyName" : "PopupRua", "StrategyType" : "Outdoor", "StrategyBudget" : 1000, "ExtraElements" : { "Latitude" : 8.1234559999999991, "Longitude" : -16.123456000000001 }, "Latitude" : 8.1234559999999991, "Longitude" : -16.123456000000001 }], "CampaignBudget" : 0 }
{ "_id" : ObjectId("5ebd8fb6ba2a34000106cccc"), "Name" : "Test Online 1", "Strategies" : [{ "_t" : ["StrategyType", "OnlineStrategy"], "StrategyName" : "OnlineGoogle", "StrategyType" : "Online", "StrategyBudget" : 2000, "ExtraElements" : { "URL" : "http:\\www.bing.com", "Latitude" : 0.0, "Longitude" : 0.0 }, "URL" : "http:\\www.bing.com" }, { "_t" : ["StrategyType", "TvStrategy"], "StrategyName" : "TVPortugal", "StrategyType" : "TV", "StrategyBudget" : 0, "ExtraElements" : { "ChannelSlots" : [{ "ChannelName" : "RTP1", "FromHour" : "13:30:00", "ToHour" : "13:30:00" }, { "ChannelName" : "TVI", "FromHour" : "15:30:00", "ToHour" : "16:30:00" }], "Latitude" : 0.0, "Longitude" : 0.0 }, "ChannelSlots" : [{ "ChannelName" : "RTP1", "FromHour" : "13:30:00", "ToHour" : "13:30:00" }, { "ChannelName" : "TVI", "FromHour" : "15:30:00", "ToHour" : "16:30:00" }] }, { "_t" : ["StrategyType", "OutdoorStrategy"], "StrategyName" : "PopupRua", "StrategyType" : "Outdoor", "StrategyBudget" : 0, "ExtraElements" : { "Latitude" : 8.1234559999999991, "Longitude" : -16.123456000000001 }, "Latitude" : 8.1234559999999991, "Longitude" : -16.123456000000001 }], "CampaignBudget" : 3000 }

但是当我尝试发送任何文件时,它会返回 415 Unsupporte Media Type。

我需要在控制器或招摇过滤器上定义什么?

标签: c#.net-coreswaggercrud

解决方案


推荐阅读