当前位置:网站首页>It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
2022-07-02 22:26:00 【My space IO】
Preface
Last , We use the smallest WEB API Realize file upload function (《 It's not easy to say love you | Use minimum WEB API File upload 》), Although client access is normal , But when open Swagger When the page is , It turns out that :

Can't use Swagger Test page .
allow Content Type
natural Swagger The page should look like this :

It seems , We need to specify Content Type:
app.MapPost("/upload",
async (HttpRequest request) =>
{
var form = await request.ReadFormAsync();
return Results.Ok(form.Files.First().FileName);
}).Accepts<HttpRequest>("multipart/form-data");
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
result ,Swagger The page becomes like this , Added a pile of Form Related properties , Only No file :

It seems , Only custom Swagger Page .
Customize OperationFilter
stay OpenAPI 3.0 in , The request for file upload can be described with the following structure (https://swagger.io/docs/specification/describing-request-body/file-upload/):

And in the Swashbuckle in , have access to IOperationFilter Interface implements the action filter , Controls how Swagger UI act .
ad locum , We will use RequestBody Object to realize the above file upload request structure .
public class FileUploadOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
const string FileUploadContentType = "multipart/form-data";
if (operation.RequestBody == null ||
!operation.RequestBody.Content.Any(x =>
x.Key.Equals(FileUploadContentType, StringComparison.InvariantCultureIgnoreCase)))
{
return;
}
if (context.ApiDescription.ParameterDescriptions[0].Type == typeof(HttpRequest))
{
operation.RequestBody = new OpenApiRequestBody
{
Description = "My IO",
Content = new Dictionary<String, OpenApiMediaType>
{
{
FileUploadContentType, new OpenApiMediaType
{
Schema = new OpenApiSchema
{
Type = "object",
Required = new HashSet<String>{ "file" },
Properties = new Dictionary<String, OpenApiSchema>
{
{
"file", new OpenApiSchema()
{
Type = "string",
Format = "binary"
}
}
}
}
}
}
}
};
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
then , Configure... In the startup code , Apply this action filter :
builder.Services.AddSwaggerGen(setup =>
{
setup.OperationFilter<FileUploadOperationFilter>();
});
- 1.
- 2.
- 3.
- 4.
This will appear as follows Swagger page :

Conclusion
today , We use IOperationFilter Solved the minimum WEB API Realize file upload Swagger Support .
Want to know more about , Please pay attention to my official account ”My IO“

边栏推荐
- Introduction to victoriametrics
- A specially designed loss is used to deal with data sets with unbalanced categories
- Market Research - current market situation and future development trend of aircraft front wheel steering system
- Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
- APP页面分享口令Rails实现
- New feature of go1.18: introduce new netip Network Library
- App page sharing password rails implementation
- A week's life
- 关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
- SQL必需掌握的100个重要知识点:使用游标
猜你喜欢

Micro service gateway selection, please accept my knees!

《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!

《Just because》阅读感受

TinyMCE visual editor adds Baidu map plug-in
![[staff] Sibelius 7.5.1 score software installation (software download | software installation)](/img/1a/4932a7931c54248c065cf8a1462d34.jpg)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)

Daily book CSO advanced road first exposed

Error in PIP installation WHL file: error: is not a supported wheel on this platform

20220702-程序员如何构建知识体系?
![[zero foundation I] Navicat download link](/img/23/e7808884152eeaf186fe756d6adac6.png)
[zero foundation I] Navicat download link

Share how to make professional hand drawn electronic maps
随机推荐
Service visibility and observability
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
Market Research - current market situation and future development trend of genome editing mutation detection kit
Daily book - low code you must understand in the era of digital transformation
使用 EMQX Cloud 实现物联网设备一机一密验证
Official announcement! The golden decade of new programmers and developers was officially released
New feature of go1.18: trylock, which has been tossed n times
技术人创业:失败不是成功,但反思是
Promise optimized callback hell
sql service 截取字符串
Market Research - current situation and future development trend of cell-based seafood market
记录一下微信、QQ、微博分享web网页功能
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
pyqt图片解码 编码后加载图片
Using emqx cloud to realize one machine one secret verification of IOT devices
An overview of the development of affective computing and understanding research
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
分享一下如何制作专业的手绘电子地图
Infrastructure is code: a change is coming
Chargement de l'image pyqt après décodage et codage de l'image