[英]NSwag Wep Api 2 multipart/form-data Attribute / file upload
I am trying to setup a Controller Method with NSwag where I can Upload a multipart/form-data file 我正在尝试使用NSwag设置Controller方法,我可以上传multipart / form-data文件
[HttpPost]
[Route("{number:long}")]
[ValidateMimeMultipartContentFilter]
[SwaggerResponse(500, typeof(string), Description = "Error")]
public async Task<IHttpActionResult> Upload(long number)
{
//My backend code for file upload
}
But I am not able to upload a file through NSwag webinterface. 但是我无法通过NSwag webinterface上传文件。 In ASP.NET Core you have a Attribute for this issue I think, but how can I get this support in Web Api 2
在ASP.NET Core中,我认为你有这个问题的属性,但是如何在Web Api 2中获得这种支持
NSwag is not supporting file upload for Web API 2 out of the box, you need to create a operation processor which creates the parameter for the file upload. NSwag不支持开箱即用的Web API 2文件上载,您需要创建一个操作处理器,为文件上载创建参数。
I've created my own Operation Processor 我已经创建了自己的操作处理器
public class SwaggerFilChunkUploadOperationProcessor : IOperationProcessor
{
public Task<bool> ProcessAsync(OperationProcessorContext context)
{
var data = context.OperationDescription.Operation.Parameters;
//File upload
data.Add(new SwaggerParameter()
{
IsRequired = true,
Name = "file",
Description = "filechunk",
Type = JsonObjectType.File,
Kind = SwaggerParameterKind.FormData
});
//custom formdata (not needed for the file upload)
data.Add(new SwaggerParameter()
{
IsRequired = true,
Name = "file-name",
Description = "the original file name",
Type = JsonObjectType.String,
Kind = SwaggerParameterKind.FormData
});
return Task.FromResult(true);
}
//defined as Attribute Usage, so you can use the attribute in your Controller
public class SwaggerFileChunkUploadAttribute : SwaggerOperationProcessorAttribute
{
public SwaggerFileChunkUploadAttribute() : base(typeof(SwaggerFilChunkUploadOperationProcessor))
{
}
}
In your controller you can now use 在您的控制器中,您现在可以使用
[ValidateMimeMultipartContentFilter]
[SwaggerFileChunkUpload]
public async Task<IHttpActionResult> Upload(long ordernumber)
{
//process your file here!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.