zoukankan      html  css  js  c++  java
  • 接口 web api Swagger 文件上传

    原文:https://github.com/domaindrivendev/Swashbuckle/issues/120
    原文:https://www.cnblogs.com/yaosj/p/10343185.html
    原文:https://blog.csdn.net/superes/article/details/1330437
    原文:https://www.cnblogs.com/carekee/articles/2094675.html



    1、FileInputAttribute,添加一个特性类,用于标识该方法是否支持文件上传

    [AttributeUsage(AttributeTargets.Method)]
    public sealed class FileInputAttribute : Attribute
    {
        public string Name { get; private set; }
        public string Description { get; private set; }
    
        public FileInputAttribute(string name, string description)
        {
            Name = name;
            Description = description;
        }
    }
    

    2、HttpAuthHeaderFilter,在swagger中,显示文件上传

    public class HttpAuthHeaderFilter : IOperationFilter
    {
        void IOperationFilter.Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            if (operation.parameters == null)
            {
                operation.parameters = new List<Parameter>();
            }
    
            var requestAttributes = apiDescription.GetControllerAndActionAttributes<FileInputAttribute>();
            foreach (var attr in requestAttributes)
            {
                operation.parameters.Add(new Parameter
                {
                    description = attr.Description,
                    name = attr.Name,
                    @in = "formData",
                    required = true,
                    type = "file"
                });
                operation.consumes.Add("multipart/form-data");
            }
        }
    }
    

    3、SwaggerConfig,注入到swagger的配置文件中

    取消注释
    c.OperationFilter<AssignOAuth2SecurityRequirements>();;
    修改为
    c.OperationFilter<HttpAuthHeaderFilter>();

    4、HomeApiController,创建一个测试

    [HttpPost, AllowAnonymous, FileInput("ImportImage", "Upload image file")]
    public string Upload()
    {
        return HttpContext.Current.Request.Files[0].FileName;
    }
    

    遗留的问题

    不知道为什么,先暂时跳过,以后有时间再仔细了解

        [HttpPost, AllowAnonymous, FileInput("ImportImage", "Upload image file")]
        public string PostFormDataImage3()
        {
            //传同样的文件 第二次请求接口时,相应的时间会异常的长
            return "123";
    
            //传同样的文件 第二次请求 不会有问题
            //return HttpContext.Current.Request.Files[0].FileName;
        }
    

    一个不是很好的解决方式,但可以解决该问题。以后有时候再找找问题的原因
    TokenAuthorizeAttribute中,添加一句代码即可int fileNum = System.Web.HttpContext.Current.Request.Files.Count;

    文件大小的限制

    maxRequestLength:最大文件上载大小,单位为KB,默认值为 4096 KB (4 MB)。

    <configuration>
      <system.web>
         <httpRuntime maxRequestLength="102400" />
      </system.web>
    <configuration>
    
  • 相关阅读:
    原始数据导入ods
    flume job
    flume拦截器
    Pandas用法总结
    NumPy用法总结
    matplotlib的使用
    【Java】Java线程中断(Interrupt)与阻塞(park)的区别
    【MySQL】MySQL中的索引原理与索引分类
    【JUC】从Lock到AQS了解Java中的锁
    【Redis】Redis 持久化之 RDB 与 AOF 详解
  • 原文地址:https://www.cnblogs.com/guxingy/p/12918434.html
Copyright © 2011-2022 走看看