Skip to content

Miltonbhowmick/copy-paste-image-field

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

COPY IMAGE - PASTE IMAGE

A mini tricks between input field and img tag in html to show an image.

GitHub Repo stars

Play it by yourself

Demo

How it works

Which logic helps

  • Use event.clipboardData or event.originalEvent.clipboardData.
  • event.clipboardData or event.originalEvent.clipboardData contains items if they have. If they contain, write var items = (event.clipboardData || event.originalEvent.clipboardData).items;.
  • Now, you have items. Its items, you can do a loop 💁‍♂️. You can see each element by index of that loop items[index].
  • We need only file item from items[index]. So, you may do a check like this if (item.kind === 'file').
  • Get the blob data of this file item by writing var blob = item.getAsFile();.
  • As it is a file, you have to read the file item. To read a file item, Javascript has a class FileReader(). Write var reader = new FileReader();.
  • How can you see If a file is not loaded 🤫?. Write reader.onload = function (event) {};.
  • Inside the above onload function, you can write document.getElementById("pastedImage").src = event.target.result; of your image html tag <img id="pastedImage"/>.
  • Then copy image and paste image and BOOM!!!.

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •