我正在尝试通过以下帖子从JavaScript的剪贴板中获取图片:https://stackoverflow.com/a/6338207/6188783
document.onpaste = function (event) {
console.log("paste triggered!");
console.log("text: " + event.clipboardData.getData("text")); // shows text if it was pasted
console.log("image: " + event.clipboardData.getData("image")); // always returns empty string p.s. I've also tried image/bmp and other formats
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
console.log(JSON.stringify(items)); // will give you the mime types
for (index in items) {
var item = items[index];
if (item.kind === 'file') {
var blob = item.getAsFile();
var reader = new FileReader();
reader.onload = function(event){
console.log(event.target.result)}; // data url!
reader.readAsDataURL(blob);
}
}
}
问题是,剪贴板数据.items和剪贴板数据.files对于文件始终为空。当我粘贴一些文本时,可以在items数组中找到它。有评论的人说,这个答案不适用于新的浏览器,但是我现在找不到解决方法。请帮忙!
谢谢!
更新
我发现从文件夹(即文件夹)复制图像时,图像没有得到处理。如果我从图像中复制图像(例如绘画),则将其推入项数组。那我该如何处理从Windows资源管理器复制的图像文件?