Shoutout to @anzharip.
This piece of code did wonders for me. We're utilizing the @anzp/azure-function-multipart
module in our code.
Here's my implementation:
HttpTrigger1/index.ts
:
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import parseMultipartFormData from "@anzp/azure-function-multipart";
const httpTrigger: AzureFunction = async function (
context: Context,
req: HttpRequest
): Promise<void> {
const { fields, files } = await parseMultipartFormData(req);
context.log("HTTP trigger function processed a request.");
const name = req.query.name || (req.body && req.body.name);
const fileContent = files.length > 0 ? Buffer.from(files[0].bufferFile).toString('utf-8') : null;
const responseMessage = {
fields,
files:[
{
...files[0],
fileContent,
},
],
};
context.res = {
body: responseMessage,
};
};
export default httpTrigger;
hello.txt
:
Hello, this is file data of hello.txt
OUTPUT
:
{
"fields": [
{
"name": "Name",
"value": "Vivek",
"nameTruncated": false,
"valueTruncated": false,
"encoding": "7bit",
"mimeType": "text/plain"
}
],
"files": [
{
"name": "Testfile",
"bufferFile": {
"type": "Buffer",
"data": [
72,
101,
108,
108,
111,
44,
32,
116,
104,
105,
115,
32,
105,
115,
32,
102,
105,
108,
101,
32,
100,
97,
116,
97,
32,
111,
102,
32,
104,
101,
108,
108,
111,
46,
116,
120,
116
]
},
"filename": "hello.txt",
"encoding": "7bit",
"mimeType": "text/plain",
"fileContent": "Hello, this is file data of hello.txt"
}
]
}
Functions:
HttpTrigger1: [GET,POST] http://localhost:7071/api/HttpTrigger1
For detailed output, run func with --verbose flag.
[2024-01-31T13:44:55.287Z] Host lock lease acquired by instance ID '000000000000000000000000AAE5F384'.
[2024-01-31T13:45:36.816Z] Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=cbd178b7-09f8-4316-b22b-1f8fd5b5a3f0)
[2024-01-31T13:45:37.065Z] HTTP trigger function processed a request.
[2024-01-31T13:45:37.371Z] Executed 'Functions.HttpTrigger1' (Succeeded, Id=cbd178b7-09f8-4316-b22b-1f8fd5b5a3f0, Duration=648ms)