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
You should catch errors if the clipboard is empty…
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.
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 :