-
-
Save darek3444/f8d866ab608367d62678a8c75992a6d1 to your computer and use it in GitHub Desktop.
Upload clipboard image to server (HTML5 / JavaScript / AJAX)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> | |
</head> | |
<body> | |
<script> | |
document.onpaste = function(event){ | |
var items = (event.clipboardData || event.originalEvent.clipboardData).items; | |
for (var i = 0 ; i < items.length ; i++) { | |
var item = items[i]; | |
if (item.type.indexOf("image") != -1) { | |
var file = item.getAsFile(); | |
console.log(file); | |
upload_file_with_ajax(file); | |
} | |
} | |
} | |
function upload_file_with_ajax(file){ | |
var formData = new FormData(); | |
formData.append('file', file); | |
$.ajax('./clipboard_js.php' , { | |
type: 'POST', | |
contentType: false, | |
processData: false, | |
data: formData, | |
error: function() { | |
console.log("error"); | |
}, | |
success: function(res) { | |
console.log("ok"); | |
} | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment