Skip to content

Instantly share code, notes, and snippets.

@solos
Created August 5, 2013 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solos/6154751 to your computer and use it in GitHub Desktop.
Save solos/6154751 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>粘贴传图Ctrl + V</title>
<script src="jquery-1.6.1.min.js" charset="utf-8"></script>
</head>
<body>
<p id="notice"></p>
<script language="javascript">
window.onload=function(){
document.body.onpaste = function(e) {
console.log(e);
var items = e.clipboardData.items;
for (var i = 0; i < items.length; ++i) {
var item = e.clipboardData.items[i];
if (items[i].kind == 'file' && items[i].type == 'image/png') {
//FileReader可以参考API
var fileReader = new FileReader();
//readAsDataURL是一个异步过程,这里提供回调方法
fileReader.onloadend = function () {
var d = this.result.substr( this.result.indexOf(',')+1);
$("body").append("<div><img src='data:image/jpeg;base64,"+ d +"'</img><input type='text' class='filename'/><input type='submit' value='上传' class='but'/>" + '</div>');
$(".but").each(function(){
$(this).click(function(){
var src = $(this).parents("div").find("img").attr("src");
var filename = $(this).parents("div").find(".filename").val();
var url = $(this).parents("div").find(".url");
$.ajax({
type: 'POST',
url: 'http://localhost/api/upload',
data: "content=" + encodeURIComponent(src) + "&filename=" + filename,
success: function(data) {
//;
}
});
})
})
};
fileReader.readAsDataURL(item.getAsFile());
break;
// Just get one
}
}
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment