DEV Community

Cover image for Drag and Drop File using Javascript
Shubham Tiwari
Shubham Tiwari

Posted on

Drag and Drop File using Javascript

Hello everyone, today I'll show you how to use HTML, TailwindCSS, and Javascript to construct a drag-and-drop file upload. I will highlight the key ideas here, and the code, together with code comments, will be made available at the end of the blog.

Let's get started...

Main Things about this Drag and Drop Component -

  • It has 3 validations - File type should be either image or pdf, the File size should be less than 5 MB and It is a must to select at least 1 file.
  • The file selected will show 4 features - File name, Size, Type, and a delete button. To delete the file and select another file.
  • All the functions and methods are separately created for better code structure and readability.
  • Validation of file type/size and adding a file is done inside "drop" event listener.
  • Validation of "No files selected" is done inside the "click" of "Next" button.
  • There is an input button "Select a file" which is used to select the file manually instead of dragging and dropping.
  • The logo of the file changes according to the file type whether it is an image or pdf file.

Codepen Demo -

THANK YOU FOR CHECKING THIS POST
You can contact me on -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com

^^You can help me by some donation at the link below Thank youšŸ‘‡šŸ‘‡ ^^
ā˜• --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/css-iswherehas-and-not-2afd

https://dev.to/shubhamtiwari909/theme-changer-with-html-and-css-only-4144

https://dev.to/shubhamtiwari909/swiperjs-3802

https://dev.to/shubhamtiwari909/going-deep-in-array-sort-js-2n90

Top comments (6)

Collapse
 
josueduenas1995 profile image
JOSUƉ DUEƑAS BOLAƑOS • Edited

Nice well explain, thank you

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Thank you

Collapse
 
suharyadi2112 profile image
Suharxxxx

nice

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Thanks

Collapse
 
adarshakm profile image
Adarshakm

good job

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Thank you