Skip to content

Instantly share code, notes, and snippets.

@magemore
Created August 10, 2016 10:13
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save magemore/3de5a66fedd146b4f2ef4660028b9ffd to your computer and use it in GitHub Desktop.
Save magemore/3de5a66fedd146b4f2ef4660028b9ffd to your computer and use it in GitHub Desktop.
upload image from clipboard on ctrl+v event on page
function uploadFile(file) {
var formData = new FormData();
formData.append("userfile", file);
var request = new XMLHttpRequest();
request.onload = function () {
if (request.status == 200) {
document.location.href='/';
} else {
alert('Error! Upload failed');
}
};
request.open("POST", "/compose/send");
request.send(formData);
}
$("body").bind('paste', function(je){
var e = je.originalEvent;
for (var i = 0; i < e.clipboardData.items.length; i++) {
var item = e.clipboardData.items[i];
console.log('Item: ' + item.type);
if (item.type.indexOf('image') != -1) {
//item.
uploadFile(item.getAsFile());
} else {
// ignore not images
console.log('Discarding not image paste data');
}
}
});
@AhsanSN
Copy link

AhsanSN commented Jan 20, 2021

Thanks alot for this.

@muhbianco
Copy link

How can i receive that in upload file php? Can u help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment