Skip to content

File uploads in website forms, done safely

Accept CVs, photos and documents through a form: the right markup, size and type limits, private storage and short-lived links.

2 min read

On this page

A file field turns a simple form into something much more useful: job applications with a CV, support requests with a screenshot, quotes with a floor plan. It also brings the hardest questions in form handling. Where do the files live, who can open them, how big may they be, and what stops someone from uploading something harmful? This guide covers the markup and the rules that keep uploads safe.

The markup

A form that sends files must use POST and multipart/form-data. Without that encoding type the browser sends only the file name.

<form action="https://formpik.com/api/in/YOUR_KEY" method="POST" enctype="multipart/form-data">
  <label for="name">Name</label>
  <input id="name" name="name" autocomplete="name" required>

  <label for="cv">Your CV (PDF, up to 5 MB)</label>
  <input id="cv" name="cv" type="file" accept=".pdf,application/pdf" required>

  <button type="submit">Send application</button>
</form>

Add multiple to let people pick several files at once; the field then sends each one under the same name. Say the accepted types and the size limit in the label, so people know before they pick a file.

Why accept is not a security check

The accept attribute only filters what the file picker shows. Anyone can choose "All files" or send a request without your page at all. Every limit has to be checked again where the files arrive.

In Formpik, a File field's accepted types and its largest file size are rules on the field, checked by the endpoint on every submission. A file that breaks them refuses the submission with a clear message for that field, which the Formpik script shows under the input. On top of that, the endpoint takes at most 10 files of 10 MB each and 25 MB per request.

Store files privately

Uploaded files are personal data more often than not. Formpik keeps them in private storage with the submission, never at a public address. When someone on your team opens a file in the Submissions inbox, they get a link that works for 15 minutes, so a forwarded or leaked link stops working on its own. The API does the same: asking for a file answers a short-lived link.

Keep the list of types short

  • Accept only what you need: PDF and Word files for a CV, images for a photo.
  • Never accept scripts, executables or HTML for anyone else to open.
  • Check the file name extension and the declared type together, and treat both as hints rather than proof of what is inside.
  • Scan files with antivirus software before opening them on a shared machine if your process allows it.

Delete files when you are done

Files follow the retention period of their form, so an application form set to keep submissions for 90 days also deletes its CVs after 90 days. Deleting a submission by hand removes its files at the same time.

Sending files from JavaScript

To upload from your own script, build a FormData from the form and send it with fetch. Do not set the Content-Type header yourself: the browser adds it with the boundary the body needs.

const form = document.querySelector('#application')

form.addEventListener('submit', async event => {
  event.preventDefault()

  const response = await fetch(form.action, {
    method: 'POST',
    headers: { Accept: 'application/json' },
    body: new FormData(form)
  })
  const result = await response.json()

  console.log(result.ok ? 'Sent' : result.message)
})

References

Keep reading

More from the blog