Skip to content

Instantly share code, notes, and snippets.

@rvhuang
Created September 7, 2017 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvhuang/ce074afe4cd59b05be3af469f2eeb4fa to your computer and use it in GitHub Desktop.
Save rvhuang/ce074afe4cd59b05be3af469f2eeb4fa to your computer and use it in GitHub Desktop.
Enable file upload button on Swagger's page.
using Swashbuckle.Swagger;
using System;
using System.Web.Http.Description;
namespace UploadingFileFromSwagger.Models
{
/* How to use:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.OperationFilter<FileUploadOperation>();
});
*/
public class FileUploadOperation : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (StringComparer.OrdinalIgnoreCase.Equals(operation.operationId, "Values_Post")) // The format is Action_Method.
{
operation.parameters.Clear();
operation.parameters.Add(new Parameter
{
name = "uploadedFile1",
@in = "formData",
description = "Upload File 1",
required = true,
type = "file"
});
operation.parameters.Add(new Parameter
{
name = "uploadedFile2",
@in = "formData",
description = "Upload File 2",
required = true,
type = "file"
});
operation.consumes.Add("multipart/form-data");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment