【问题标题】:File upload with Swagger 5.0.0-rcX and .Net-Core 3使用 Swagger 5.0.0-rcX 和 .Net-Core 3 上传文件
【发布时间】:2021-08-06 04:43:40
【问题描述】:

我正在使用 .NetCore 3 和 Swagger 5.0.0-rc4。我正在尝试使用 Swagger 上传文件(图像),但它不起作用,因为 IOperationFilter 甚至 Swashbuckle.AspNetCore.Swagger 中的应用方法缺少一些属性。例如 NonBodyParameter 和 Consumes 不会在 Swagger 5.0 中退出

有没有人遇到同样的问题或试图解决它?

public class FileOperationFilter : IOperationFilter
    {

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.OperationId.ToLower() == "apivaluesuploadpost")
            {
                operation.Parameters.Clear();
                operation.Parameters.Add(new **NonBodyParameter**
                {
                    Name = "uploadedFile",
                    In = "formData",
                    Description = "Upload File",
                    Required = true,
                    Type = "file"
                });
                operation.**Consumes**.Add("multipart/form-data");
            }
        }
    }

【问题讨论】:

  • Please do not post images of code。相反edit您的问题并将代码添加为格式正确的markdown
  • 你能澄清一下文件上传是如何不起作用的吗?预期结果和实际结果是什么?
  • 要使 File-upload 在 Swagger 中工作,您必须对 FileOperationFilter 进行一些自定义并在 StartUp 类中进行配置,但这种自定义无法在 Swagger 5.0 中实现,因为似乎删除了一些属性来自 Swagger 5.0。我在没有自定义的情况下实现 IFormFile 时得到的结果如下: Fetch errorundefined /swagger/v1/swagger.json
  • 我遇到了同样的问题;自升级以来,选择文件对话框按钮已消失。所以不知道如何测试和验证它是否正常工作。

标签: c# asp.net-core file-upload swagger .net-core-3.0


【解决方案1】:

缺少参数的AS 现在这些已更改为OpenApiParameter 和OpenApiOperation。

public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.OperationId == "MyOperation")
            {
                operation.Parameters.Clear();
                operation.Parameters.Add(new OpenApiParameter
                {
                    Name = "formFile",
                    In = ParameterLocation.Header,
                    Description = "Upload File",
                    Required = true,
                    Schema= new OpenApiSchema
                    {
                        Type="file",
                        Format="binary"
                    }
                }); 
                var uploadFileMediaType = new OpenApiMediaType()
                {
                    Schema = new OpenApiSchema()
                    {
                        Type = "object",
                        Properties =
                    {
                        ["uploadedFile"] = new OpenApiSchema()
                        {
                            Description = "Upload File",
                            Type = "file",
                            Format = "binary"

                        }
                    },
                        Required = new HashSet<string>()
                    {
                        "uploadedFile"
                    }
                    }
                };
                operation.RequestBody = new OpenApiRequestBody
                {
                    Content =
                {
                    ["multipart/form-data"] = uploadFileMediaType
                }
                };


            }
        }

【讨论】:

    【解决方案2】:

    我设法在 Swashbuckle.AspNetCore 6.1.5 中解决了这个问题

    Swagger 自动将 IFormFile 识别为 multipart/form-data 媒体类型。

    您只需删除过滤器类并从控制器中的参数中删除[FromForm][FromBody] 属性。

    void Post([FromForm] IFileForm file)

    void Post(IFileForm file)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-04
      • 2019-06-26
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 2017-04-30
      • 1970-01-01
      相关资源
      最近更新 更多