Skip to content

Instantly share code, notes, and snippets.

@an9er
Created October 11, 2016 01:53
Show Gist options
  • Save an9er/8f95f5665d489bf658380149b5661b8f to your computer and use it in GitHub Desktop.
Save an9er/8f95f5665d489bf658380149b5661b8f to your computer and use it in GitHub Desktop.
// 将图片资源显示到页面中
function showImage(imageData) {
var reader = new FileReader();
reader.onload = function(e){
var img = new Image();
img.src = e.target.result;
document.body.appendChild(img);
};
// 读取图片文件
reader.readAsDataURL(imageData);
}
document.querySelector("#input").addEventListener("paste", function(e){
var clipboardData = e.clipboardData,
items,
item,
types;
if(clipboardData){
items = clipboardData.items;
if(!items){
return;
}
item = items[0];
// 保存在剪贴板中的数据类型
types = clipboardData.types || [];
for(var i = 0; i < types.length; i++ ){
if(types[i] === "Files"){
item = items[i];
break;
}
}
// 判断是否为图片数据
if( item && item.kind === 'file' && item.type.match(/^image\//i) ){
var blob = item.getAsFile();
showImage(blob);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment