Skip to content

Instantly share code, notes, and snippets.

@gaving
Created August 11, 2011 23:16
Show Gist options
  • Save gaving/1141046 to your computer and use it in GitHub Desktop.
Save gaving/1141046 to your computer and use it in GitHub Desktop.
post a blob directly to imgur
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>paste blob to imgur</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).bind('paste', function(e) {
var e = e.originalEvent;
if (e && e.clipboardData && e.clipboardData.getData) {
var items = e.clipboardData.items;
var blob = items[0].getAsFile();
if (!blob || !blob.type.match(/image.*/)) {
alert('Cant get as file or not an image');
return;
}
var fd = new FormData();
fd.append("image", blob);
fd.append("key", "cd5eaa68ce7de375ad377c439847de87");
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.imgur.com/2/upload.json");
xhr.onload = function() {
var url = JSON.parse(xhr.responseText).upload.links.original;
console.log(url);
alert(url);
}
xhr.send(fd);
}
});
</script>
</head>
<body>
Paste here!
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment