I have a form with multiple fields and some attachments. I need to submit it to an Azure function using http trigger (Post). Any guidance or sample how I can I write this Azure function using C#/.NET to accept fields data And Attachments?
Asked
Active
Viewed 2,378 times
1
-
Here's [something I've found](https://www.cyotek.com/blog/upload-data-to-blob-storage-with-azure-functions). – aepot Aug 08 '20 at 13:53
-
@aepot: this one is about creating a file from a body of the request. in my case it is submitting a form that has multiple fields (say: name, email, phone) PLUS some attached files – Emad Gabriel Aug 08 '20 at 13:58
-
Maybe [this](https://stackoverflow.com/a/62681494/12888024) then. To attach files you must encode it to Base64 and add as text to the params Dictionary. – aepot Aug 08 '20 at 14:17
-
@aepot Thanks, but what i am looking for is how to write the azure function to "accept" the fields and attachments. Not how to "send" the request – Emad Gabriel Aug 08 '20 at 14:37
-
[Documentation](https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library) – aepot Aug 08 '20 at 14:45
1 Answers
1
You can refer to the sample code below. I did some tests and it can receive parameters.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace AcceptFormData
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var formdata = await req.ReadFormAsync();
string name = formdata["name"];
string email = formdata["email"];
string phone = formdata["phone"];
var image = req.Form.Files["file"];
log.LogInformation(name);
log.LogInformation(email);
log.LogInformation(phone);
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
}
The request body in postman is constructed like this:
I did it with reference to this blog. I think this can solve your problem, if not, you can tell me.

Frank Borzage
- 6,292
- 1
- 6
- 19
-
Works fine. Was wondering if there is a better way to parse the formdata collection directly to a viewmodel object instead of reading and assigning properties one by one – Emad Gabriel Aug 13 '20 at 14:58