<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Brett Connolly</title>
    <description>The latest articles on DEV Community by Brett Connolly (@brett84c).</description>
    <link>https://dev.to/brett84c</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F85979%2Ff588e14d-3b38-44ef-b38c-788c1e3e9082.jpeg</url>
      <title>DEV Community: Brett Connolly</title>
      <link>https://dev.to/brett84c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brett84c"/>
    <language>en</language>
    <item>
      <title>Process FormData file sent to Node server</title>
      <dc:creator>Brett Connolly</dc:creator>
      <pubDate>Sun, 19 Mar 2023 15:52:16 +0000</pubDate>
      <link>https://dev.to/brett84c/process-formdata-file-sent-to-node-server-5d8o</link>
      <guid>https://dev.to/brett84c/process-formdata-file-sent-to-node-server-5d8o</guid>
      <description>&lt;p&gt;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:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Buffer.from(await file.arrayBuffer())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;form.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form id="fileForm"&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;label for="pdf_file"&amp;gt;File Upload&amp;lt;/label&amp;gt;
      &amp;lt;input type="file" name="pdf_file" /&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;button type="submit"&amp;gt;Send File&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;form.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById("fileForm").addEventListener("submit", (e) =&amp;gt; {
  e.preventDefault();
  const formData = new FormData(e);
  fetch("/", {
    method: "POST",
    body: formData
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;server.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/', async (request, response) =&amp;gt; {
  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
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>file</category>
      <category>upload</category>
      <category>formdata</category>
      <category>attach</category>
    </item>
  </channel>
</rss>
