Use a
FormData
object to send the file via an AJAX request to the server.
Using FormData Objects - Web APIs | MDN[
^]
Using Fetch - Web APIs | MDN[
^]
document.onpaste = async (evt) => {
const dT = evt.clipboardData || window.clipboardData;
const formData = new FormData();
for (let i = 0; i < dT.files.length; i++) {
formData.append("PastedFile", dT.files[i]);
}
const response = await fetch("path/to/controller/action", {
method: "POST",
body: formData
});
if (!response.ok) {
} else {
const result = await response.json();
...
}
};