C H*_*all 7 swagger-ui azure-functions openapi
有没有一种方法可以装饰一个函数,以便生成的 swagger UI 允许用户选择要上传的文件
我有一个新的函数应用程序,其中包含上传文件并存储该文件所需的代码,但我不知道如何让 OpenAPI 扩展在生成的 swagger UI 中正确记录该 API。
谢谢。
这是您可以尝试的解决方法之一
例如,我考虑将我的响应文件保存为图像/png,这是我的函数 OpenAPI 函数
[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;
}
Run Code Online (Sandbox Code Playgroud)
它的模型
public class MultiPartFormDataModel
{
public byte[] FileUpload { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是图像文件的输出:

这是文本文件的输出:

参考: 通过 Swagger UI 进行 Azure Functions 二进制数据传输
| 归档时间: |
|
| 查看次数: |
2248 次 |
| 最近记录: |