当前位置:网站首页>JS to read the picture of the clipboard

JS to read the picture of the clipboard

2022-06-23 00:45:00 Ziwei front end

<script>
function test(evt){  
    //for chrome  
    var clipboardData = evt.clipboardData;  
    for(var i=0; i<clipboardData.items.length; i++){  
        var item = clipboardData.items[i];  
        if(item.kind=='file'&&item.type.match(/^image\//i)){  
            //blob Is the binary image data in the clipboard   
            var blob = item.getAsFile(),reader = new FileReader();  
            // Definition fileReader Callback after reading data   
            reader.onload=function(){  
                var sHtml='<img src="'+event.target.result+'">';//result Should be base64 Encoded picture   
                document.getElementById('dd').innerHTML += sHtml;  
            }  
            reader.readAsDataURL(blob);// use fileReader Read binary picture , The callback function defined above will be called after completion   
        }  
    }  
}  
</script>
<textarea id="t" onpaste="test(event)" cols=60 rows=5></textarea>  
<div id="dd"></div>

原网站

版权声明
本文为[Ziwei front end]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222049582952.html