使用HTTP内容上传的API文件上传不大张旗鼓 [英] API File Upload using HTTP content not exposed in swagger

查看:120
本文介绍了使用HTTP内容上传的API文件上传不大张旗鼓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将swagger接口实现到现有的Web API中.当前的API控制器公开了一个异步上传功能,该功能使用Request.Content异步传输图像. 这篇文章.

I am implementing a swagger interface into an existing web API. The current API controller exposes an async upload function which uses the Request.Content to transport an image asynchronously. The code that has been used is explained in this article.

我的api控制器:

    [HttpPost]
    [Route("foo/bar/upload")]
    public async Task<HttpResponseMessage> Upload()
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }
        var provider = await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider());
        NameValueCollection formData = provider.FormData;
        HttpResponseMessage response;
        //access files  
        IList<HttpContent> files = provider.Files;
        if (files.Count > 0)
        {
            HttpContent file1 = files[0];
            using (Stream input = await file1.ReadAsStreamAsync())
            {
                object responseObj = ExternalProcessInputStream(input)
                response = Request.CreateResponse(HttpStatusCode.OK, responseObj);
            }
        }
        else 
        {
            response = Request.CreateResponse(HttpStatusCode.BadRequest);
        }
        return response;
    }

这很不错,但是当我通过招摇暴露时,我有一个无参数函数,使用时会返回错误.

This works dandy, but when i expose this through swagger i have a parameterless function, which returns an error when used.

我的问题是如何提供适当的值来测试此方法?

My question is how can supply a proper value to test this method with?

推荐答案

您需要添加一个自定义IOperationFilter来处理此问题.

You'll need to add a custom IOperationFilter to handle this.

假设您有一个像这样的控制器:

Given you have a controller like so:

    [ValidateMimeMultipartContentFilter]
    [HttpPost, Route("softwarepackage")]
    public Task<SoftwarePackageModel> UploadSingleFile()
    {

        var streamProvider = new MultipartFormDataStreamProvider(ServerUploadFolder);
        var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith<SoftwarePackageModel>(t =>
        {
            var firstFile = streamProvider.FileData.FirstOrDefault();

            if (firstFile != null)
            {
                // Do something with firstFile.LocalFileName
            }

            return new SoftwarePackageModel
            {

            };
        });

        return task;
    }

然后,您需要创建一个Swashbuckle.Swagger.IOperationFilter,以将文件上传参数添加到函数中,例如:

You then need to create an Swashbuckle.Swagger.IOperationFilter to add a file upload parameter to your function like:

    public class FileOperationFilter : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            if (operation.operationId.ToLower() == "softwarepackage_uploadsinglefile")
            {
                if (operation.parameters == null)
                    operation.parameters = new List<Parameter>(1);
                else
                    operation.parameters.Clear();
                operation.parameters.Add(new Parameter
                {
                    name = "File",
                    @in = "formData",
                    description = "Upload software package",
                    required = true,
                    type = "file"
                });
                operation.consumes.Add("application/form-data");
            }
        }
    }

在Swagger配置中,您需要注册过滤器:

And in your Swagger config you'll need to register the filter:

config.EnableSwagger(c => {... c.OperationFilter<FileOperationFilter>(); ... });

为此,我还添加了一个FilterAttribute来过滤出多部分内容:

To top this up, I also added a FilterAttribute to filter out Multipart content:

public class ValidateMimeMultipartContentFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (!actionContext.Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {

    }

}

这篇关于使用HTTP内容上传的API文件上传不大张旗鼓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆