// ctrl + v自然会触发paste事件,根本不用你自己去监听ctrl+v
document.addEventListener('paste',function(event){
//ClipboardEvent
// 获取文件
console.dir(event.clipboardData.files);
// 获取文本
console.dir(event.clipboardData.getData('text'));
});
获取剪贴板文件时有两个坑:
坑1: console.dir(event.clipboardData)打印clipboardData时,files总是为空,此时,你不妨试试打印 console.dir(event.clipboardData.files);
坑2:如果console.dir(event.clipboardData.files);还是为空,那么你是不是从电脑上复制了一个文件然后在界面上粘贴的?这样的不行,因为文件没有在剪贴板,什么样的文件会在剪贴板呢?一种是你自己通过操作clipboard对象主动放进去的,另一种是比如微信的截图,这种文件,或者一个图片你打开它,再右键复制,而不是在文件的缩略图上复制。
扫码关注,获取更多好料: