Guides

How to create a file upload form with formcarry in 5 steps

Learn how to create a file upload ready HTML form in 5 steps, super simple!

How to create a file upload form with formcarry in 5 steps
On this page8 sections
  1. After you read this guide
  2. Step 1: Create your HTML Form
  3. Step 2: Structure your form data
  4. Step 3: Point your form to backend
  5. Step 4: Integrate your form with formcarry
  6. Step 5: Collect submissions
  7. Uploading files from your form using Javascript
  8. Conclusion

One of the most time consuming thing about forms is definitely uploading files, sometimes you may need an HTML form with file upload functionality, in this guide we are going to start by as simple as possible than we are going to jump to advanced techniques

After you read this guide#

  • You can create a file upload form in 5 steps, under 3 minutes
  • You can get email notifications from your HTML form
  • You can export your contact form submissions as CSV and JSON
  • You don't have to deal with any back end code stuff (Php, Go, Node.js etc.)

Step 1: Create your HTML Form#

First, you'll need an HTML form in your web page, which is not a big deal, a basic HTML upload form will look like this.

HTML
1<form action="#" method="POST" enctype="multipart/form-data">
2	<label for="nameInput">Name</label>
3	<input type="text" id="nameInput" />
4
5	<label for="messageInput">Message</label>
6	<textarea id="messageInput" cols="30" rows="2"></textarea>
7
8	<input type="file" id="pictureInput" />
9
10	<button type="submit">Submit</button>
11</form>

Your form element have to include enctype="multipart/form-data" to upload files

A basic form in HTML is just a static thing that's only obligated to deliver the user input to a back-end script. You have to point the form to some script to actually process the data.

if you want to have a fully styled form you can use our free form generator tool to generate a customized form code.

By default, file input only allows you to choose a single file from your device, if you want it to allow multiple files then you can use the multiple attribute

HTML
<input type="file" id="pictureInput" multiple />

Step 2: Structure your form data#

In order to send your form data to a script you need to name each of your fields so the script can match the values with the names (or the key).

You can add as many fields as you want.

Let's uniquely name each of our fields using name attribution.

JSX
1<form action="#" method="POST" enctype="multipart/form-data">
2	<label for="nameInput">Name</label>
3  	<input type="text" id="nameInput" name="name" />
4
5	<label for="messageInput">Message</label>
6	<textarea id="messageInput" cols="30" rows="2" name="message"></textarea>
7
8	<label for="pictureInput">Picture</label>
9	<input type="file" id="pictureInput" name="picture" />
10
11	<button type="submit">Submit</button>
12</form>

Now we have a file upload form that's ready to deliver the data to a script.

Step 3: Point your form to backend#

There are two ways to handle this step, this part is the most time consuming stuff due to you have to check files, rename them uniquely save to database and upload to somewhere along with you'll also probably going to need a email server to send emails, which you always have to make sure that emails are delivered.

Option 1: Create your own form back-end#

You need to have a form backend to collect submissions and files from your form, this is typically done by some backend languages such as PHP, Node.js, Go etc.

We have a detailed guide on How to create a form backend with file upload

How to create an HTML contact form with file upload to Amazon S3 | Step-by-step guideHow to create an HTML contact form with file upload to Amazon S3 using Node.js and Express.js/blog/how-to-create-a-html-contact-form-with-file-upload/

Take a look at that post if you want to build it on your own

Option 2: Use formcarry - File upload ready form backend#

formcarry is a free, no-code form backend for your HTML forms, in this tutorial I'm going to show you how can you create a file upload form under 3 minutes while it at least takes 1 hours if you do it on your own. Also you can get notification emails from your form, protected from spam by default and integrate your submission data with other apps using Zapier.

Sign Up to formcarry for free, then click Add New to get your unique form backend URL

Then copy your unique form endpoint

Step 4: Integrate your form with formcarry#

Configure tab of a form in formcarry showing the form endpoint URL

Copy your endpoint and paste it into action part of your form

Diff
- <form action="#" method="POST" enctype="multipart/form-data">
+ <form action="https://formcarry.com/s/{Your-Unique-Endpoint}" method="POST" enctype="multipart/form-data">

Step 5: Collect submissions#

Let's try out our form:

Now, let's take a look at our dashboard:

That's all you need to upload files when you are using formcarry, that's how deadly simple it is. We have covered the basics, now let's jump into something a little bit advanced.


Uploading files from your form using Javascript#

Uploading files with raw HTML form is not so time consuming, but if you are trying to upload it with javascript it's possible that you are going to spend some time on figuring out what's going wrong.

First add our form id="formcarryForm"

Diff
- <form action="https://formcarry.com/s/{Your-Unique-Endpoint}" method="POST" enctype="multipart/form-data">
+ <form id="formcarryForm" method="POST" enctype="multipart/form-data">

Important note before we switch back to code#

Common mistake while implementing a file upload in javascript is setting Content-Type as multipart/form-data while this is partly true, it's also very wrong approach.

Any content-type starts with multipart/* is required to have boundary, based on my own experiences and the support request we got from formcarry, we found out the ideal way to upload files from javascript is using the FormData class

JavaScript
const formElement = document.getElementById('formcarryForm')

const data = new FormData(formElement)

this will get each field from your form, but if you want to add some additional data, that you don't want to store in the HTML, you can add your additional data like that;

JavaScript
const formElement = document.getElementById('formcarryForm')

const data = new FormData(formElement)

data.append('language', window.navigator.language)

Uploading files from your form using Fetch API#

Fetch API is an embedded request API for javascript, it's widely supported among modern browsers, check Fetch API compability.

Add this script before the </body> tag

HTML
1<script>
2  const formcarryForm = document.getElementById('formcarryForm')
3
4  const sendFormData = async (event) => {
5    // this will prevent your form to redirect to another page.
6    event.preventDefault();
7		
8    // get our form data
9    const formData = new FormData(formcarryForm)
10    // add some additional data if you want
11    // formData.append('language', window.navigator.language)
12
13    fetch('https://formcarry.com/s/{Your-Unique-Endpoint}', {
14      method: 'POST',
15      body: formData,
16      headers: {
17		// you don't have to set Content-Type
18		// otherwise it won't work due to boundary!
19        Accept: 'application/json'
20      }
21    })
22    // convert response to json
23    .then(r => r.json())
24    .then(res => {
25      console.log(res);
26    });
27  }
28
29  // Bind our sendFormData function to our forms submit event
30  formcarryForm.addEventListener('submit', sendFormData);
31</script>

Uploading files from your form using Axios#

Axios is an promise based HTTP client for the browser and node.js, it's lightweight and a common library for making HTTP requests, it's a nice alternative for Fetch API

Add this script before the </body> tag

HTML
1<script>
2  const formcarryForm = document.getElementById('formcarryForm')
3
4  const sendFormData = async (event) => {
5    // this will prevent your form to redirect to another page.
6    event.preventDefault();
7		
8	// get our form data
9	const formData = new FormData(formcarryForm);
10	// add some additional data if you want
11	// formData.append('language', window.navigator.language)
12
13    axios
14      .post("https://formcarry.com/s/{Your-Unique-Endpoint}", formData, {
15        headers: { 
16			// you don't have to set Content-Type
17			// otherwise it won't work due to boundary!
18			Accept: "application/json" 
19		},
20      })
21      .then(function (response) {
22        console.log(response);
23      })
24      .catch(function (error) {
25        // this is an error related to request
26        console.log(error);
27      });
28  };
29
30  // Bind our sendFormData function to our forms submit event
31  formcarryForm.addEventListener('submit', sendFormData);
32</script>

Uploading Files from your form with React#

Using FormData class in React is a little bit tweaky, but let's take a look how to upload files with React.

We need to use useRef to access our form element, here's a sample component for you to upload files with React:

JSX
1import React, { useRef } from "react"
2
3const Form = () => {
4  // create a Ref to access our form element
5  const formRef = useRef(null)
6
7  const sendFormData = async (event) => {
8    // this will prevent your form to redirect to another page.
9    event.preventDefault();
10
11    if(!formRef.current){
12      console.log('something wrong with form ref')
13      return
14    }
15		
16    // get our form data
17    const formData = new FormData(formRef.current)
18
19    // add some additional data if you want
20    // formData.append('language', window.navigator.language)
21
22    fetch('https://formcarry.com/s/{Your-Unique-Endpoint}', {
23      method: 'POST',
24      body: formData,
25      headers: {
26		// you don't have to set Content-Type
27		// otherwise it won't work due to boundary!
28        Accept: 'application/json'
29      }
30    })
31    // convert response to json
32    .then(r => r.json())
33    .then(res => {
34      console.log(res);
35    });
36  }
37
38  return (
39	// bind formRef to our form element
40    <form ref={formRef} onSubmit={sendFormData}>
41      <label htmlFor="nameInput">Name</label>
42      <input type="text" id="nameInput" name="name" />
43
44      <label htmlFor="messageInput">Message</label>
45      <textarea id="messageInput" name="message"></textarea>
46
47      <label htmlFor="pictureInput">Picture</label>
48      <input type="file" id="pictureInput" name="picture" />
49
50      <button type="submit">Submit</button>
51    </form>
52  )
53}
54
55export default Form

That's all! here's the live demo: codesandbox


Conclusion#

I hope this guide was helpful for you, at formcarry our mission is providing an easy way to handle your HTML forms also we are trying to release form related in-depth guides, make sure you've subscribed our newsletter to get the latest posts.

Nusu AlabugaFounder, formcarry

Nusu Alabuga founded formcarry in 2017 and still runs it: the ingest pipeline, the spam filtering, the email delivery and the incident tracking that watches customers' forms. A product engineer by trade, with years of work in payments, security, invoicing and banking infrastructure.

WebsiteXLinkedInGitHub

Keep reading

LET'S GET STARTED!

Hassle Free Forms

Create your form endpoint and start collecting submissions

for your HTML forms, it only takes 2 minutes to set up your form

formcarry dashboard.
Your email address|