Written by: Geoffrey Callaghan

How to create file upload forms in Hugo

How To Create File Upload Forms In Hugo

To create a file upload form in Hugo using FabForm, you’ll need to set up your Hugo site, integrate FabForm for form handling, and add the necessary configurations and scripts for file uploads.

Step-by-Step Guide

  1. Set Up Your Hugo Site: If you haven’t already, create a new Hugo site:

    hugo new site my-hugo-site
    cd my-hugo-site
  2. Add FabForm to Your Site: Install FabForm by adding the following script to the head section of your layouts/_default/baseof.html or layouts/partials/head.html file:

    <script src="https://cdn.jsdelivr.net/npm/fabform@latest/dist/fabform.min.js" defer></script>
  3. Create a File Upload Form: Add your file upload form in the content file where you want the form to appear, for example, content/contact.md. Use the following markdown content:

    ---
    title: "Contact"
    ---
    
    <h1>Contact Us</h1>
    
    <form id="contact-form" action="https://fabform.io/f/YOUR_FORM_ID" method="POST" enctype="multipart/form-data">
        <div>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" required>
        </div>
        <div>
            <label for="email">Email:</label>
            <input type="email" id="email" name="email" required>
        </div>
        <div>
            <label for="message">Message:</label>
            <textarea id="message" name="message" required></textarea>
        </div>
        <div>
            <label for="file">Upload File:</label>
            <input type="file" id="file" name="file" required>
        </div>
        <button type="submit">Submit</button>
    </form>
    
    <script>
        document.getElementById('contact-form').addEventListener('submit', function(event) {
            event.preventDefault();
            var form = event.target;
            fabform.submitForm(form);
        });
    </script>

    Replace YOUR_FORM_ID with your actual FabForm form ID.

  4. Configure FabForm: You need to sign up for FabForm and create a new form to get your form ID. Configure the form on the FabForm website to handle file uploads.

  5. Test Your Form: Start your Hugo server and test the form:

    hugo server

    Open your browser and navigate to http://localhost:1313/contact/ to see the form and test file uploads.

Explanation

  • Form Structure: The form includes fields for name, email, message, and file upload.
  • Form Attributes: enctype="multipart/form-data" is crucial for file uploads.
  • FabForm Integration: The FabForm script (fabform.min.js) is loaded to handle form submissions. The action attribute in the form points to your FabForm endpoint.
  • JavaScript Submission: The form submission is handled via JavaScript to leverage FabForm’s functionality.

Additional Tips

  • Styling: You can style your form using CSS to match your site’s design.
  • Validation: FabForm handles basic validation, but you can add custom validation if needed.
  • Confirmation Message: Customize the submission confirmation message on FabForm’s dashboard or handle it in your JavaScript code.

By following these steps, you can successfully create a file upload form in Hugo using FabForm, allowing users to submit files along with their form data.