温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用Swagger怎么实现文件上传功能

发布时间:2020-12-31 15:11:47 来源:亿速云 阅读:417 作者:Leah 栏目:开发技术

利用Swagger怎么实现文件上传功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

经常使用swagger,可以通过设置[ProducesResponseType]标记接口的返回信息;swagger也能通过接口的参数列表,自动获得发送的数据结构信息。

不过有一个例外,就是上传文件的时候,设置了[Consumes]的内容为multi-part/form-data,但是swagger并不能正常感知是上传文件的。代码是这个样子的:

[Consumes("multipart/form-data")]
[ODataRoute]
[HttpPost]
public async Task<ActionResult> Post(IFormCollection collection)
{
 var file = collection.Files[0];
 if(file != null)
 {
 var filename = DateTime.Now.ToString("yyyyMMddHHmmss") + file.FileName;
 var path = Path.Combine(_webHostEnvironment.WebRootPath, "Files", filename);
 using FileStream fileStream = new FileStream(path, FileMode.Create);
 await file.CopyToAsync(fileStream);
 var uri = "Files/" + filename;
 var fileEntity = new Models.File { Url = uri, LastModified = DateTime.Now };
 _homeworkDataContext.Files.Add(fileEntity);
 await _homeworkDataContext.SaveChangesAsync();
 return Created(WebUtility.UrlEncode(uri), fileEntity);
 }
 return BadRequest();
}

实际上,swagger一直提示,上传的内容是一个array类型,当然API是没有问题的,可以通过POSTMAN进行发送,不过不能在网页上直接操作,总觉得心里有点不太舒服。

利用Swagger怎么实现文件上传功能

方法

搜索了一下办法,比较靠谱的,就是通过增加一个IOperationFilter来实现目的。

// CODE FROM https://www.talkingdotnet.com/how-to-upload-file-via-swagger-in-asp-net-core-web-api/
public class FileUploadOperation : IOperationFilter
{
 public void Apply(Operation 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");
 }
 }
}

然后,在services.ConfigureSwaggerGen()参数中,添加

options.OperationFilter<FileUploadOperation>();

方法的原理是通过重写操作某个特定API的的过滤器,来实现对返回内容的操作。

此方法适用于OAS2,实质上是实现了这里的规范要求。

我已经用上.NET 5.0了,自带了swagger都支持的是OpenAPI 3,这个方法不好用了。不过思想应该相同,首先看看OpenAPI 3的规范,文件上传需要定义为:

requestBody:
 content:
 multipart/form-data:
 schema:
 type: object
 properties:
  fileName:
  type: string
  format: binary

这个套路和OpenAPI 2完全不一样,需要重新设置requestBody才行。我们按照要求改造代码。

public class FileUploadOperation : IOperationFilter
{
 public void Apply(OpenApiOperation operation, OperationFilterContext context)
 {
 //判断上传文件的类型,只有上传的类型是IFormCollection的才进行重写。
 if (context.ApiDescription.ActionDescriptor.Parameters.Any(w => w.ParameterType == typeof(IFormCollection)))
 {
  Dictionary<string, OpenApiSchema> schema = new Dictionary<string, OpenApiSchema>();
  schema["fileName"] = new OpenApiSchema { Description = "Select file", Type = "string", Format = "binary" };
  Dictionary<string, OpenApiMediaType> content = new Dictionary<string, OpenApiMediaType>();
  content["multipart/form-data"] = new OpenApiMediaType { Schema = new OpenApiSchema { Type = "object", Properties = schema } };
  operation.RequestBody = new OpenApiRequestBody() { Content = content };
 }
 }
}

看完上述内容,你们掌握利用Swagger怎么实现文件上传功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI