How to paste an image from a clipboard using JavaScript?

Estimated read time 1 min read

You can paste an image from the clipboard into a webpage using the Clipboard API and JavaScript. Here’s an example:

document.addEventListener('paste', function(event) {
  let items = (event.clipboardData || event.originalEvent.clipboardData).items;
  for (let item of items) {
    if (item.kind === 'file') {
      let blob = item.getAsFile();
      let URLObj = window.URL || window.webkitURL;
      let source = URLObj.createObjectURL(blob);
      let image = new Image();
      image.src = source;
      document.body.appendChild(image);
    }
  }
});

Note that this feature is currently only supported in modern browsers.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply