Facebook SDK

Drag & Drop or Browse - File Upload Tutorial using HTML CSS and JavaScript

Hello Coders, In this tutorial, We are going to learn how to create drop and drop feature or browse to upload file feature using html, css and vanilla javascript.

What is drag & drop file upload ?

Most of the websites on internet, In order to upload file over the internet we used to use the browse feature previously. But nowadays drag and drop file upload feature has also become quite common. This feature was developed to make user experience more easier. Its so simple that, if you want to upload a file to a website all you have to do now is, just simply drag the file and its done. The file will get uploaded to the website or server.

What will be the final result of this project ?

  • User should be able to Upload image file with extensions jpeg, jpg, png.
  • If user uploads a file which is not supported, we will show an alert saying the file is not supported.
  • when the user uploads a file. The uploaded image should be displayed.

In this article we are going to learn how to build this feature using html css and vanilla javascript. Watch and read the video tutorial as well as this step by step article which will teach you how to build this project. You can download the complete source code of this project clicking on the download button at the bottom.

Drag and Drop or Browse - File upload tutorial in HTML CSS and JavaScript | Video Tutorial

In this video tutorial, I have shown how to build it in step by step. Watch the video tutorial and follow along with it if you are complete beginner and finding it hard to understand this article. 

You may also like : 

Drag and Drop or Browse - File Upload tutorial with HTML CSS and vanilla JavaScript | Source Codes

Follow the below step by step guide to build this project. I have also provided all the necessary source codes to make you understand it better. You can also download all the source codes completely by clicking on the download button at the bottom of this article.

Files required for this project :

  • index.html - For HTML
  • styles.css - For Styling
  • script.js - For adding drag and drop or browse feature

How do I create Drag and Drop or Browse File upload feature in HTML? Free HTML Source Code
Lets start with html. Inside your index.html file, Create the basic structure of the html document and add html, head, title, body tags. Follow below steps.

Step 1 : We will be using the font awesome cdn to import an icon from thier library. You can see in the above demo that file icon is actually imported from the font awesome. I have provided the import code for it below.

Step 2 : Now link both our styles.css file as well script.js file to our html document.

Step 3 : We will create a div with class name 'container' and inside it we will create another div with class name 'drag-area'. This drag area is going to be the area where we will drag our file to upload. Start typing the below code to complete the html part.

Output after HTML :

How do I design Drag and Drop or Browse File upload feature in CSS? Free CSS Source Code
Lets start with CSS. I have provided the source code of css below. Follow below step.

Step 4 : Inside the styles.css file. We will be using a custom google font i.e Poppins with weight 200, 300, 400, 500. Below I have provided the code for it. Start typing the css code from top to bottom and observe the changes it makes.

Output after CSS : 

How do I create Drag and Drop or Browse File upload feature in JavaScript? Free JavaScript Source Code
JavaScript is the main important thing in this project. Using javascript we can make it work as we want it to. Follow the below steps.

Step 5 : Now its time for JavaScript. First We will start by grabbing all the DOM elements that are required. We will also create a variable to store the file.

Step 6 : Now we will implement the drag and drop feature first. Create a separate function to store and display the file. Inside the function we will define what are all the valid file types which we want to accept for the upload. 

    We will define three event listeners, one for keep tracking of when the file is inside the drag area. One for tracking when it leaves the drag area and lastly one for when the user drops the file inside the drag area.

Step 7 : Lets implement the browse feature. When the browse button is clicked we will simulate a click on the file input which opens up file explorer. We will listen for the change event and we will call the display function once user chooses a file.

Result of Drag & Drop or Browse - File Upload Feature : 

Download Source Code of Drag and Drop or Browse File Upload Feature with HTML, CSS and JavaScript. 

Download Code Files


Post a Comment

Previous Post Next Post