Azure Functions OpenApi 扩展文件上传 POST 请求

Azure Functions OpenApi extensions File Upload POST request

有没有一种方法可以修饰函数,使生成的 swagger UI 允许用户select 上传文件

我有一个新的功能应用程序,其中包含上传文件和存储该文件所需的代码,但我不知道如何获取 OpenAPI 扩展以正确记录 API 在生成的 swagger UI.

谢谢。

这是您可以尝试的解决方法之一

例如,我考虑将我的响应归档为 image/png,这是我的函数 OpenAPI Function

    
        [FunctionName(nameof(SampleFunction.Run))]
        [OpenApiOperation(operationId: "run", tags: new[] { "multipartformdata" }, Summary = "Transfer image through multipart/formdata", Description = "This transfers an image through multipart/formdata.", Visibility = OpenApiVisibilityType.Advanced)]
        [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
        [OpenApiRequestBody(contentType: "multipart/form-data", bodyType: typeof(MultiPartFormDataModel), Required = true, Description = "Image data")]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "image/png", bodyType: typeof(byte[]), Summary = "Image data", Description = "This returns the image", Deprecated = false)]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "POST", Route = "form/multipart")] HttpRequest req,
            ILogger log)
        {
            var files = req.Form.Files;
            var file = files[0];

            var content = default(byte[]);
            using (var ms = new MemoryStream())
            {
                await file.CopyToAsync(ms).ConfigureAwait(false);
                content = ms.ToArray();
            }

            var result = new FileContentResult(content, "image/png");

            return result;
        }

它的型号

public class MultiPartFormDataModel
    {
        public byte[] FileUpload { get; set; }
    }

这是图像文件的输出:

这是文本文件的输出:

参考资料: Azure Functions Binary Data Transfer via Swagger UI