gpt4 book ai didi

c# - 如何使用 Swagger UI 下载文件

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:26 28 4
gpt4 key购买 nike

我有 rest Controller ,它以流文件作为内容返回 HttpResponseMessage,如下所示:

public class MyController : ApiController
{
public HttpResponseMessage GetFile(string id)
{
try {
var stream = fileSystemUtils.GetFileStream(filePath); //Get Stream
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = stream;
return response;
}
catch (FileNotFoundException)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
}
}

当我在浏览器中通过 ULR 调用此方法时,一切正常,我可以下载此文件。现在我想使用 Swagger UI 下载它。有什么简单的方法可以做到这一点吗?

最佳答案

如果其他人正在寻找这个,我做了以下操作:

public class FileOperation : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
if (operation.OperationId.ToLower() == "apifileget")
{
operation.Produces = new[] { "application/octet-stream" };
operation.Responses["200"].Schema = new Schema { Type = "file", Description = "Download file"};
}
}
}

//In the startup...
services.AddSwaggerGen(c =>
{
//missing code...
c.OperationFilter<FileOperation>();
});

关于c# - 如何使用 Swagger UI 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44118717/

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