I struggled to find much online about how to simply process a FormData file (PDF, in this case) sent to a Node server to be attached to an email or used for other things. This is a very basic example but the key part is this line on the server:
Buffer.from(await file.arrayBuffer())
form.html
<form id="fileForm">
<div>
<label for="pdf_file">File Upload</label>
<input type="file" name="pdf_file" />
</div>
<button type="submit">Send File</button>
</form>
form.js
document.getElementById("fileForm").addEventListener("submit", (e) => {
e.preventDefault();
const formData = new FormData(e);
fetch("/", {
method: "POST",
body: formData
});
});
server.js
app.post('/', async (request, response) => {
const values = await request.formData();
const file = values.get("pdf_file");
const processedFile = Buffer.from(await file.arrayBuffer());
//attach to email or do other things
};
Top comments (0)