File asp.core Api swagger client NSwag

https://stackoverflow.com/questions/43844261/what-is-the-correct-way-to-download-a-file-via-the-nswag-code-generator-angular

1. Microsoft.AspNetCore.StaticFiles // To determine MimeType
2. NSwag.Annotations 

services.AddSwaggerGen(options =>
{
    // Swagger Configurations
    options.MapType<FileContentResult>(() => new OpenApiSchema
    {
        Type = "file"
    });
});

[HttpGet("Get_File")]
[SwaggerOperation("Get_File", OperationId = "Get_File")]
[SwaggerResponse(200, type: typeof(FileContentResult))]
[ProducesResponseType(typeof(FileContentResult), 200)]
public IActionResult GetFile()
{
    var filePath = "D:\\11.pdf"; // get file full path based on file name
    if (!System.IO.File.Exists(filePath))
    {
        return BadRequest();
     }
 
     return File(System.IO.File.ReadAllBytesAsync(filePath).Result, "application/octet-stream", "11.pdf");
}
Kategoria: