You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am sruggling with File downloads.
Unfurtunately, the basic implementation just adds an FileResponse as thew 200er response, whiich is not the expected state of affairs.
I build this OperationFilter to repleace the FileResult with a File download, but the UI still tries to display the file.
Any ideas?
I would be interested to integrate this filter into SB itself, if we get it working and I get some pointer where to add it.
`
public class SwaggerFileOperationFilter : IOperationFilter
{
private static readonly Type FileResultType = typeof(FileResult);
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var anyFileStreamResult = context.ApiDescription.SupportedResponseTypes.Any(x => IsSubclassOfFileResult(x.Type));
if (!anyFileStreamResult)
{
return;
}
var kvp = new KeyValuePair<string, OpenApiMediaType>("application/octet-stream", new OpenApiMediaType()
{
Schema = new OpenApiSchema()
{
Type = "file"
}
});
operation.Responses["200"].Content.Clear();
operation.Responses["200"].Content.Add(kvp);
}
private static bool IsSubclassOfFileResult(Type toCheck)
{
return toCheck.IsSubclassOf(FileResultType) || toCheck == FileResultType;
}
}`
The text was updated successfully, but these errors were encountered:
The following commit adds out-of-the-box support for FileResult responses - fefe652.
However, there's a couple of gotcha's to be aware of.
Firstly, ApiExplorer (the ASP.NET Core metadata component that SB is built on) DOES NOT surface the FileResult type by default and so you need to explicitly tell it to with the Produces attribute:
[HttpGet("{fileName}")]
[Produces("application/octet-stream", Type = typeof(FileResult))]
public FileResult GetFile(string fileName)
Secondly, if you want the swagger-ui to display a `Download file" link after you're operation will need to return a Content-Type of "application/octet-stream" or a Content-Disposition of "attachement". See swagger-api/swagger-ui#4250
I am sruggling with File downloads.
Unfurtunately, the basic implementation just adds an FileResponse as thew 200er response, whiich is not the expected state of affairs.
I build this OperationFilter to repleace the FileResult with a File download, but the UI still tries to display the file.
Any ideas?
I would be interested to integrate this filter into SB itself, if we get it working and I get some pointer where to add it.
`
public class SwaggerFileOperationFilter : IOperationFilter
{
private static readonly Type FileResultType = typeof(FileResult);
The text was updated successfully, but these errors were encountered: