Open In App

How to create input field that accept CSV file in HTML ?

CSV (Comma Separated Values) is a commonly used file format for storing tabular data. To allow users to upload CSV files to a web application, we can create an input field that accepts CSV files in HTML. There are different approaches to achieve this, ranging from the simple “accept” attribute in the input tag to more advanced third-party libraries.

To create an input field that accepts CSV files in HTML, we can use the “input” tag with a type=”file” attribute to create a file upload input field. We can add the accept=”.csv” attribute to limit file types to CSV only. We can also add a name attribute to the input tag to provide a name for the file upload field. When a file is uploaded, it can be accessed using JavaScript and parsed as CSV data. This input field can be used to allow users to upload CSV files to a web application.



Syntax:

<input type="file" accept=".csv">

There are several approaches to creating an input field that accepts CSV files in HTML:



Using the “accept” attribute:  The simplest way is to use the “accept” attribute in the input tag to limit the file type to CSV. 

Using the input element with the “accept” attribute: 

Example: This example shows the use of the above-explained approach.




<html>
<head>
    <title>
          input field that accept CSV file in HTML 
      </title>
</head>
  
<body>
    <input type="file" name="csvFile" accept=".csv">
</body>
</html>

Output:

csv file using accept method

Explanation: In this example, we create a standard HTML form with an input field for file upload. We add the “accept” attribute to the input tag and set it to “.csv” to restrict file selection to CSV files. We also add the “name” attribute to the input tag to identify the file on the server side. Finally, we specify the “method” and “action” attributes of the form to handle the file upload.

Using the drag-and-drop: To allow users to select CSV files using drag-and-drop in HTML, you can use the HTML5 drag-and-drop API along with JavaScript to handle the drag-and-drop events and file selection.

Using the drag-and-drop feature: 

Example: This example shows the use of the above-explained approach.




<!DOCTYPE html>
<html>
<head>
    <title>csv file select</title>
</head>
  
<body>
    <button id="csv-dropzone" type="file">
          Drop CSV file here
      </button>
  
    <script>
        const csvDropzone = document.getElementById("csv-dropzone");
  
        csvDropzone.addEventListener("dragover", function (event) {
            event.preventDefault();
            csvDropzone.classList.add("dragover");
        });
  
        csvDropzone.addEventListener("dragleave", function (event) {
            event.preventDefault();
            csvDropzone.classList.remove("dragover");
        });
  
        csvDropzone.addEventListener("drop", function (event) {
            event.preventDefault();
            csvDropzone.classList.remove("dragover");
            const csvFile = event.dataTransfer.files[0];
  
            if (csvFile.type === "text/csv" || 
                csvFile.type === "application/vnd.ms-excel") {
                alert("csv file has been selected");
                handleCsvFile(csvFile);
            } else {
                alert("Please drop a CSV file.");
            }
        });
    </script>
</body>
</html>

Output:

select csv file using drop down mathod

Explanation: In this example, a button can be used to select a CSV file. The JavaScript code adds event listeners to the button for “dragover”, “dragleave”, and “drop” events. The “dragover” and “dragleave” events add and remove a CSS class to indicate that a file is being dragged over the button. The “drop” event retrieves the CSV file from the drag event and checks its type. If the file is a CSV file, an alert is displayed and the file is handled. If the file is not a CSV file, an alert is displayed asking the user to drop a CSV file.

Using a third-party library: There are several third-party libraries available that provide a more advanced CSV file upload functionality. But I will use the Dropzone.js library to enable users to upload CSV files using drag-and-drop in HTML. 

The code creates a form with a dropzone element that users can drag and drop CSV files onto. The Dropzone.js library is initialized with options that limit the file selection to CSV files only and allow for only one file to be uploaded at a time.

Using a third-party library:

Example: This example shows the use of the above-explained approach..




<!DOCTYPE html>
<html>
<head>
    <title>csv file</title>
    <link rel="stylesheet" href=
    <script src=
      </script>
</head>
  
<body>
    <form id="csv-upload-form">
        <div class="dropzone" id="csv-dropzone">
            <div class="dz-message">
                Drop CSV file here
            </div>
        </div>
    </form>
  
    <script>
        const csvDropzone = new Dropzone("#csv-dropzone", {
            url: "/upload",
            acceptedFiles: ".csv",
            maxFiles: 1,
            init: function () {
                this.on("success", function (file, response) {
                    console.log(response);
                });
            }
        });
    </script>
</body>
</html>

Output:

select csv file  using third party library 

Explanation: This code creates a form with a Dropzone area that allows the user to drag and drop a CSV file onto it. The accepted file type is limited to CSV files using the acceptedFiles option in the Dropzone() constructor. When a file is successfully uploaded, the success event is triggered, and the console.log() function outputs the server response. This implementation uses the Dropzone.js library to handle the drag-and-drop file selection and uploading.


Article Tags :