Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.0.0-beta Download File does not work #1034

Closed
ChristianSauer opened this issue Feb 5, 2019 · 1 comment
Closed

5.0.0-beta Download File does not work #1034

ChristianSauer opened this issue Feb 5, 2019 · 1 comment
Milestone

Comments

@ChristianSauer
Copy link

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;
    }
}`
@domaindrivendev
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants