Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What pits will be encountered when using minimum WEB API to upload files and how to solve them?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not quite understand the knowledge points of this article "what pits will be encountered and how to solve them when using minimum WEB API to achieve file upload", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this article entitled "what pits will be encountered and how to solve them when using minimum WEB API to upload files?"

Foreword:

Before .NET 6, implementing file upload was simple:

[HttpPost ("upload")] public async Task Upload (IFormFile file) {/ / a pair of file performs return Ok (file.FileName);}

However, when you use the minimum WEB API of .NET 6 to achieve the same functionality, you encounter a lot of holes unexpectedly.

I. implementation code

After converting to a minimum WEB API to implement, the code looks like this:

App.MapPost ("/ upload", async (IFormFile file) = > {return Results.Ok (file.FileName);})

However, a HTTP 415error is returned when calling with the same client code:

Second, allow Content Type

This is because the Content-Type used when uploading files is not the default application/json, but multipart/form-data

In dotnet/aspnetcore 's issues, we found the relevant solution:

Modify the code as follows:

App.MapPost ("/ upload", async (IFormFile file) = > {return Results.Ok (file.FileName);}) .Accepts ("multipart/form-data")

As a result, a 415 error was returned. Even weirder, there is an extra stack of error messages:

Third, the emergence of BUG

Therefore, I want to find the problem by looking at the code.

Using the error message "Expected a supported JSON media type but got", we located the source code file Http/Http.Extensions/src/RequestDelegateFactory.cs:

However, there is a big difference in the code between the master branch and the v6.0.0 branch. For example, the master branch has IFormFile-related code, but not at all on v6.0.0:

Looking at the submission history of the file, we found a submission like this:

Based on the submission, we found the corresponding issue:

It seems that to support the IFormFile parameter, we will have to wait until November 2022 (the expected release date of .NET 7)!

IV. Solution

Fortunately, the issue also provides a workaround solution, passing in the HttpRequest parameter:

App.MapPost ("/ upload", async (HttpRequest request) = > {var form = await request.ReadFormAsync (); return Results.Ok (form.Files.First () .filename);})

Verification was successful.

The above is about the content of this article on "what pits will be encountered and how to solve them when using the minimum WEB API to achieve file upload". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report