Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Azure Function .NET5 and HttpRequestData, how to handle file upload (form data)?

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?

like image 488
thankyoussd Avatar asked Nov 22 '25 10:11

thankyoussd


1 Answers

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.

enter image description here

When debug

enter image description here

Below is my test code.

[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;
    }
like image 77
Jason Pan Avatar answered Nov 25 '25 04:11

Jason Pan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!