当前位置:网站首页>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“

边栏推荐
- An overview of the development of affective computing and understanding research
- Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
- sql service 截取字符串
- Unity3d learning notes 4 - create mesh advanced interface
- Market Research - current market situation and future development trend of aircraft audio control panel system
- Chargement de l'image pyqt après décodage et codage de l'image
- 发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
- Blue Bridge Cup Eliminate last one (bit operation, code completion)
- "New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
- 540. Single element in ordered array
猜你喜欢
![[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)
![[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)](/img/4c/c8dae41fc2eb18b5153cf36861fc7d.jpg)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
![[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)](/img/f7/cb41d159e5c5ef3f4f1b9468d52ccc.jpg)
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)

PIP audit: a powerful security vulnerability scanning tool
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register

sql service 截取字符串

Kubernetes resource object introduction and common commands (4)

分享一下如何制作专业的手绘电子地图

The difference between include < > and include ""

Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
随机推荐
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
Chargement de l'image pyqt après décodage et codage de l'image
如何访问kubernetes API?
[Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Servicemesh mainly solves three pain points
Pointer and string
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
服务可见可观测性
Daily book - low code you must understand in the era of digital transformation
Micro service gateway selection, please accept my knees!
PHP微信抢红包的算法
TinyMCE visual editor adds Baidu map plug-in
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
Web侧防御指南
540. Single element in ordered array
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Market Research - current market situation and future development trend of aircraft audio control panel system
How to prevent your jar from being decompiled?