使用.net在aws lamda注释框架中读取表单数据

问题描述 投票:0回答:1

在函数内部,我想获取表单数据文件

我无法在api请求中使用表单文件

[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Post, "/create")]
public async Task<IActionResult> AddEntity()
{
    try
    {

        return new OkObjectResult(new { message = "data added" });
    }
    catch (Exception ex)
    {
        throw;
    }

}```
.net file aws-lambda multipartform-data form-data
1个回答
0
投票

AWS Lambda 不直接支持 IFormFile。相反,使用 APIGatewayHttpApiV2ProxyRequest 访问原始多部分/表单数据负载,并使用 HttpMultipartParser 等库对其进行解析。这是基本设置:

[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Post, "/create")]
public async Task<IActionResult> AddEntity([FromBody] APIGatewayHttpApiV2ProxyRequest request)
{
    var contentType = request.Headers["content-type"];
    if (contentType.Contains("multipart/form-data"))
    {
        var formData = ParseMultipartFormData(request.Body, contentType);
        var fileContent = formData["file"]; // Process file here
    }
    return Ok(new { message = "Data added successfully" });
}

使用Postman等工具发送多部分/表单数据

© www.soinside.com 2019 - 2024. All rights reserved.