question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to allow a file download?

See original GitHub issue

Several of my API methods result in a file download. How can I get Swashbuckle to download a file through the UI for these methods?

I’ve added an IOperationFilter with the following Apply method to add a “produces” attribute to certain methods:

public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
    if (apiDescription.ID == "GETapi/Templates/{id}")
    {
        operation.produces.Add("application/octet-stream");
    }
}

But when I execute this method through the Swashbuckle UI it displays the file contents in the “Response Body” textarea. Is there a way of getting a file to actually download?

Thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:22 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
gauicommented, May 5, 2018

Weird, we are having the same problem. We have the latest version of SB 2.4.0. Why is this closed?

We have the following function:

[HttpGet("{id}")]
[ProducesResponseType(typeof(FileStreamResult), 200)]
public async Task<IActionResult> GetFileById(string id)

Which produces the following Swagger Spec:

'/Files/{id}':
get:
  tags:
	- Files
  operationId: FilesByIdGet
  consumes: []
  produces:
	- text/plain
	- application/json
	- text/json
  parameters:
	- name: id
	  in: path
	  required: true
	  type: string
  responses:
	'200':
	  description: Success
	  schema:
		$ref: '#/definitions/FileStreamResult'

This is not correct, so when I generate a client with AutoRest or NSwag, it tries to serialize the data as JSON.

I monkey patched it with this operation filter and it works fine:

public class SwaggerFileOperationFilter : IOperationFilter
{
	public void Apply(Operation operation, OperationFilterContext context)
	{
		var anyFileStreamResult = context.ApiDescription.SupportedResponseTypes
			.Any(x => x.Type == typeof(FileStreamResult));

		if (anyFileStreamResult)
		{
			operation.Produces = new[] { "application/octet-stream" };
			operation.Responses["200"].Schema = new Schema { Type = "file" };
		}
	}
}
4reactions
domaindrivendevcommented, Jun 30, 2015

@centur - you can wire up an IOperationFilter (see readme for details). It would look something like this …

public class UpdateFileDownloadOperations : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        if (operation.operationId == "FileDownload_GetFile")
        {
            operation.produces = new[] { "application/octet-stream" };
            operation.responses["200"].schema = new Schema { type = "file" };
        }
    }
}

I’ve used this to create a valid spec, but it seems the UI still doesn’t support the downloads yet …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Download a file - Computer - Google Chrome Help
Download a file ; At the top right, click More More · Settings ; Click Privacy and security · Site Settings ; Click...
Read more >
Automatic file download notifications in Windows
Tip: Don't want notifications for automatic file downloads? Go to your notifications settings and then, under Get notifications from these senders, turn off ......
Read more >
How to Block or Unblock Downloads in Google Chrome
Open Google Chrome on your mobile device. · Tap the three-dot icon. · Choose Privacy and Security. · Tap on Safe Browsing. ·...
Read more >
Cannot download a file from the Internet? Do this!
In Security Settings, scroll down to Downloads. Set File download to Enable. Scroll down a bit more and you will see Miscellaneous. Here...
Read more >
How to enable Automatic file download in Windows 10 ...
1 Answer 1 ... When this button is greyed out, it means you have not blocked any apps from requesting automatic file downloads....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found