4

I'm trying to retrieve the Files elements from the ClipboardEvent in Angular when copy/pasting an image to a textaera.

Using the (paste)="onPaste($event)" binding, when looking to the console I only get back empty files array:

enter image description here

Here is the stackblitz example to reproduce the issue.

6
  • What browser are you using? I can assess the FileList of your StackBlitz in Chrome but not Edge. Commented Feb 15, 2020 at 10:09
  • That's strange, I tried it on Firefox (73.0) and Chrome (80.0) on Windows 10 v1909 and both are giving me an empty array when right clicking the image/copy and pasting in the textaera. :/ Commented Feb 15, 2020 at 15:41
  • Actually it didn’t give the correct file name. I could get the value with: let files = e.clipboardData.files; console.log(files[0].name); but it had image.png for the name Commented Feb 15, 2020 at 16:12
  • Well at least you got something. :) What browser version are you using? Commented Feb 15, 2020 at 16:55
  • I'm not on that PC now, so can't check. Was latest Chrome On Windows. What details do you need of the file? Name or more? Commented Feb 15, 2020 at 17:02

1 Answer 1

15
+50

Don't care about the console log. it's just a bug with the console.

Try this :

onPaste(e: any ) {
    const items = (e.clipboardData || e.originalEvent.clipboardData).items;
    let blob = null;
    for (const item of items) {
      if (item.type.indexOf('image') === 0) {
        blob = item.getAsFile();
        console.log(blob); // Prints your files
      }
    }
}

Stackblitz Example : Link

PS : If you want the "name" of pasted items, you'll need to solicit input from the user. because Clipboard API doesn't support it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.