Created
August 5, 2013 09:55
-
-
Save solos/6154751 to your computer and use it in GitHub Desktop.
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> | |
<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