我正在 AspNet Core 5 中编写一个控制器方法,该方法应该以流的形式返回文件。
[HttpGet]
[Route("/docs/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> GetDocumentAsync([FromRoute][Required] Guid? id)
{
var (stream, fileExtension) = await _dmsService.OpenDocumentAsync(id.Value, FileAccess.Read);
return File(
stream,
contentType: "application/octet-stream",
fileDownloadName: $"{id}{fileExtension}",
enableRangeProcessing: false);
}
到目前为止一切正常,但现在我想记录 API。
我使用 Swashbuckle.AspNetCore 6.1.4 生成 OpenAPI 描述,该描述应该用于生成客户端。
当我返回某个对象时,使用
ActionResult<T>
作为返回类型很容易。stream
?
[ProducesResponseType(typeof(FileStreamResult), (int)HttpStatusCode.OK)]
我使用 NSWAG 从 MapGet 自动创建客户端代码。
builder.MapGet(
"download/{path}",
async ( string path) =>
{
var mimeType = "audio/wav";
return Results.File(path);
})
.Produces(StatusCodes.Status404NotFound)
.Produces((int)StatusCodes.Status200OK, typeof(FileResult))
.WithName($"GET {EndPointName} by Name")
.WithTags(EndPointTag);