gpt4 book ai didi

api - 如何从 swashbuckle 中的请求正文上传多部分文件?

转载 作者:行者123 更新时间:2023-12-04 19:20:44 27 4
gpt4 key购买 nike

我应该如何在 .net 核心中设置 swashbuckle 以便我可以从请求正文上传文件(多部分,表单值模型绑定(bind)已禁用)?

我尝试了什么(显然在 swashbuckle 启动配置中添加了 FileUploadHelper):

public class FileUploadHelper : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
if (operation.OperationId.ToLower() == "apifileuploadpost")
{
operation.Parameters.Clear();
operation.Parameters.Add(new NonBodyParameter()
{
Name = "File",
In = "formData",
Description = "uploaded file",
Type = "file",
Required = true,
});
operation.Consumes.Add("multipart/form-data");
}
}
}


[HttpPost, DisableRequestSizeLimit]
[DisableFormValueModelBinding]
public async Task<IActionResult> Upload()
{
int id = await Request.StreamFile(fileService);
return CreatedAtAction(nameof(GetFileInfo), id);
}

StreamFile 是一种扩展方法,它将请求正文中的文件内容保存在文件内容实体 (byte[]) 中,并创建一个带有文件内容 ID 和一些附加信息(名称、描述等)的文件信息实体,但只返回生成的 ID。我只想能够在大摇大摆中单击上传按钮,选择文件并获取返回的 id 或错误响应。我不使用 IFileForm,表单值模型绑定(bind)被禁用(根据 asp.net 核心文档中使用流式传输的大文件上传)并且文件直接来自请求正文,因此我不在上传 Controller 方法中传递任何与文件相关的参数,只有“这个 HttpRequest”。这在 Swashbuckle 中是否可行?

最佳答案

您的 FileUploadHelper类看起来不错。我认为问题在于 OperationId是不同的。它应该是方法的名称Upload .我根据您的创建了一个示例并更改了 FileUploadHelper而是按路线匹配,我认为这更清楚。

这是适用于我的版本的代码片段。

Controller :

[Route("api/[controller]")]
[ApiController]
public class FileUploadController : ControllerBase
{
[HttpPost, DisableRequestSizeLimit]
[DisableFormValueModelBinding]
[Consumes("multipart/form-data")] // You can specify consumes here and it gets automatically added also to swagger
public async Task<IActionResult> Upload()
{
int id = await Request.StreamFile(fileService);
return CreatedAtAction(nameof(GetFileInfo), new { id = id }, null);
}

[HttpGet]
[Route("{id}")]
public async Task<IActionResult> GetFileInfo(int id)
{
return new JsonResult(new { id = id });
}
}

文件上传助手

public class FileUploadHelper : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
if (context.ApiDescription.RelativePath == "api/FileUpload")
{
operation.Parameters.Clear();
operation.Parameters.Add(new NonBodyParameter()
{
Name = "File",
In = "formData",
Description = "uploaded file",
Type = "file",
Required = true,
});;
}
}
}

使用添加的 Swagger 设置启动:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "File Upload API", Version = "v1" });
c.OperationFilter<FileUploadHelper>();
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "File Upload API");
});
}
}

这就是它在 UI 中的样子。

Swashbuckle file upload

关于api - 如何从 swashbuckle 中的请求正文上传多部分文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292806/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com