javascript - 如何从 Internet Explorer 中的剪贴板获取 base64 编码图像?

标签 javascript clipboard internet-explorer-11

我搜索了很多但没有找到从剪贴板获取 base64 编码的数据。我可以捕获粘贴事件,然后用这个将事件分配给变量

clipBoard = e.clipboardData ? e.clipboardData : window.clipboardData;

Chrome ;我可以得到已经粘贴的打印屏幕,就像这样

if (clipBoard.types[0] == "Files") {
    var blob = clipBoard.items[0].getAsFile();

    var reader = new FileReader();
    reader.onload = function(event){
    console.log(event.target.result);
    }; // data url!
    reader.readAsDataURL(blob);
}

但在 ie 11 中,clipBoard 变量没有“项目”或“类型”。我将上传那个图像服务器,但我没有得到 base64 编码的数据。

最佳答案

有可能...在任何网站上 :) 但是,没有跨浏览器的方法。

Chrome 和 Opera(很可能是 Safari,但我现在无法测试)中,您可以访问您在问题中所写的剪贴板。事实上,这种方法只是 Chromium 包的解决方法 Issue 31426 .

下面的代码实现了这个功能。按 Alt-PrtScr,单击编辑器字段并粘贴。我只是打印图像数据;例如,在实际程序中,我可以将它发送到我的服务器以进行进一步处理。

$(document).ready(function() {
  $('#editor').on('paste', function(e) {
    var orgEvent = e.originalEvent;
    for (var i = 0; i < orgEvent.clipboardData.items.length; i++) {
      if (orgEvent.clipboardData.items[i].kind == "file" && orgEvent.clipboardData.items[i].type == "image/png") {
        var imageFile = orgEvent.clipboardData.items[i].getAsFile();
        var fileReader = new FileReader();
        fileReader.onloadend = function() {
          $('#result').html(fileReader.result);
        }
        fileReader.readAsDataURL(imageFile);
        break;
      }
    }
  });
});
#editor {
  width: 500px;
  min-height: 40px;
  border: solid 1px gray;
  padding: 4px;
}

#resultcnt {
  width: 100%;
  margin-top: 16px;
}

#result {
  display: block;
  max-width: 90%;
  margin: 16px 0 32px 0;
  font-size: 12px;
  color: blue;
  overflow: visible;
  word-break: break-all;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='editor' contenteditable=true></div>
<div id='resultcnt'>Copyed image src:<br />
  <div id='result'></div>
</div>

IEFirefox 中,您可以使用不同的方法获得相同的结果。幸运的是,这些浏览器可以毫无问题地将打印屏幕粘贴到编辑器中,因此您根本不需要访问剪贴板。您只需监听粘贴事件并使用间隔捕获图像已创建但仍未呈现的时间点。然后您只需获取图像源并清空编辑器。

下面的代码实现了这个算法。当您在 IE 或 Firefox 中运行它时,结果将与之前示例在 Chrome 和 Opera 中的结果相同:

<script type="text/javascript">
$(document).ready(function() {
  
  $('#editor').on('paste', function (e) {
    $('#editor').empty();
		var waitToPastInterval = setInterval(function () {
			if ($('#editor').children().length > 0) {
				clearInterval(waitToPastInterval);
        $('#result').html($('#editor').find('img')[0].src);
        $('#editor').empty();
			}
		}, 1);  
  });
    
});
</script>
<style  type="text/css">
#editor{
  width: 500px;
  min-height: 40px;
  border: solid 1px gray;
  padding: 4px;
}
#resultcnt{
  width: 100%;
  margin-top: 16px;
}
#result{
  display: block;
  max-width: 90%;
  margin: 16px 0 32px 0;
  font-size: 12px;
  color: blue;
  overflow: visible;
  word-break: break-all;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='editor' contenteditable=true></div>
<div id='resultcnt'>Copyed image src:<br />
  <div id='result'></div>
</div>

关于javascript - 如何从 Internet Explorer 中的剪贴板获取 base64 编码图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28644340/

相关文章:

javascript - 在 Node js 中定义模型时,Schema 不是构造函数

Javascript:如何提取样式中动态变化的数字的值?

javascript - 如何使用 JavaScript 将富文本内容复制到剪贴板?

javascript - 将 HTML 内容添加到用户剪贴板的跨浏览器技术

wpf - 剪贴板更改通知?

css - SVG - 圆形标记在所有浏览器中的大小不同 (IE11)

css - Internet Explorer 上的 Angular 6——Angular Material/Flex 布局不会居中?

css - 即 11 :active CSS selector blocked by child elements and :active CSS selector siblings are not selected

javascript - 为什么 Date.now() | 0 与 Date.now() 不同

javascript - jQuery 幻灯片效果与 jQuery UI 位置冲突