File Uploads

Frontend SELF EN
Level 9 , Lesson 5
Available

11.1 The <input type="file"> Element

Web apps often require users to upload files to the server. This can be handy for uploading documents, images, videos, and other data types. HTML provides the <input type="file"> element to create file upload interfaces. Let's dive into using this element and its attributes like multiple and accept.

The <input type="file"> element creates an interface element that lets users select files from their device to upload to the server. It's one of the key form elements in HTML, allowing the integration of file upload functionalities in web applications.

Main Attributes of the <input type="file"> Element

  1. type="file" — Defines that the element is meant for file selection.
  2. name — Sets the name of the element, which will be used when form data is sent to the server.
  3. multiple — Allows the user to select multiple files at once.
  4. accept — Specifies the types of files that can be selected using MIME types or file extensions.

Examples of Using the <input type="file"> Element

Simple Example

The simplest example of a file upload element without additional attributes:

HTML
    
      <form action="/upload" method="post" enctype="multipart/form-data">
        <label for="file">Choose a file:</label>
        <input type="file" id="file" name="file">
        <button type="submit">Upload</button>
      </form>
    
  

11.2 The multiple Attribute

The <input type="file"> element supports several attributes that enhance its functionality. Let's take a look at two of them: multiple and accept.

The multiple attribute allows users to select multiple files for upload. When this attribute is added, users can pick multiple files in the file selection dialog.

Example Usage:

HTML
    
      <form action="/upload" method="post" enctype="multipart/form-data">
        <label for="files">Choose files:</label>
        <input type="file" id="files" name="files" multiple>
        <button type="submit">Upload</button>
      </form>
    
  
This is Useful

In the file selection dialog, users can select and upload multiple files simultaneously by holding the Ctrl (Cmd on Mac) or Shift key.

11.3 The accept Attribute

The accept attribute restricts the types of files that can be selected by the user. This is useful for ensuring that users upload only allowed file types (e.g., only images or documents).

Example Usage:

HTML
    
      <form action="/upload" method="post" enctype="multipart/form-data">
        <label for="image">Choose an image:</label>
        <input type="file" id="image" name="image" accept="image/*">
        <button type="submit">Upload</button>
      </form>
    
  

The accept attribute restricts the types of files that can be selected. This can be done by MIME types or file extensions. In the example above, users can only select image files.

Examples of accept Attribute Values

Restricting by File Type:

  • accept="image/*": any images
  • accept="video/*": any video files
  • accept="audio/*": any audio files

Restricting by File Extension:

  • accept=".jpg/.jpeg/.png": only JPEG and PNG images
  • accept=".pdf/.doc/.docx": only PDF and Word documents

11.4 Client-Side File Processing

Sometimes, you might need to process files on the client side before sending them to the server. For example, you could display image previews or check file size. For such tasks, JavaScript is used.

Example of Image Preview:

HTML
    
      <form action="/upload" method="post" enctype="multipart/form-data">
        <label for="image">Choose an image:</label>
        <input type="file" id="image" name="image" accept="image/*">
        <div id="preview"></div>
        <button type="submit">Upload</button>
      </form>
    
  
JavaScript
    
      document.getElementById('image').addEventListener('change', function(event) {
        const preview = document.getElementById('preview');
        preview.innerHTML = '';

        const files = event.target.files;

        for (let i = 0; i < files.length; i++) {
          const file = files[i];
          const reader = new FileReader();

          reader.onload = function(e) {
            const img = document.createElement('img');
            img.src = e.target.result;
            img.style.maxWidth = '200px';
            preview.appendChild(img);
          }
          reader.readAsDataURL(file);
        }
      });
    
  

Explanation

  • FileReader: used for reading the contents of files on the client side
  • reader.onload: event that triggers when a file is fully read
  • reader.readAsDataURL(file): method that reads the file and encodes it as a Data URL, allowing it to be displayed in an <img> tag
1
Task
Frontend SELF EN, level 9, lesson 5
Locked
File Upload
File Upload
1
Task
Frontend SELF EN, level 9, lesson 5
Locked
Image Upload
Image Upload
1
Survey/quiz
Form Elements, level 9, lesson 5
Unavailable
Form Elements
Form Elements
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION