Reputation: 2451
The older Azure Function gives access to HttpRequest
, which allows us to access the uploaded files via req.Form.Files
etc.
The isolated .NET5 Azure Function uses HttpRequestData
instead, which does not give access to the Form
. How do I extract the uploaded files posted to the function?
Upvotes: 20
Views: 10290
Reputation: 21918
You can add <PackageReference Include="HttpMultipartParser" Version="5.0.0" />
in your .csproj file. And use var parsedFormBody = MultipartFormDataParser.ParseAsync(req.Body);
, you will get your files.
In postman.
When debug
[Function("test")]
public static HttpResponseData Run1([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
FunctionContext executionContext
)
{
// get query params
var testvalue=executionContext.BindingContext.BindingData["testparams"];
// get form-body
var parsedFormBody = MultipartFormDataParser.ParseAsync(req.Body);
var file=parsedFormBody.Result.Files[0];
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to Azure Functions!");
return response;
}
Upvotes: 20