skip to Main Content

I am trying to read a file from the clipboard using the Clipboard API according to the manual , but my browser throws the error.

I am trying to read a file from the clipboard using the Clipboard API in this way:

const clipboard = await window.navigator.clipboard.read();
console.debug(clipboard);

The browser throws an error when executing this code:

Uncaught (in promise) DOMException: No valid data on clipboard.

Can you tell me how to make sure that this error does not occur and it is possible to receive files from the clipboard?

2

Answers


  1. You should catch errors if the clipboard is empty…

    try {
      const clipboard = await navigator.clipboard.read();
      console.debug(clipboard);
    } catch (error) {
      console.error('Error reading clipboard:', error);
    }
    

    Note not all browsers actually support this feature as of now, so there’s that as well permissions; there’s a bit to the APIs capabilities.

    Login or Signup to reply.
  2. In order to ensure that this error does not occur and its possible to receive files from the clipboard you can use ? operator. To check the availability of a parameter. This is also known as the Optional Chaining Operator.

    Refer to the below code for usage :

      const clipboard = navigator?.clipboard?.read();
      console.log(clipboard);
    
     
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search