如何从 Internet Explorer 中的剪贴板获取 base64 编码图像?
How do i get base64 encoded image from clipboard in internet explorer?
我搜索了很多但没有找到从剪贴板获取 base64 编码的数据。我可以捕获粘贴事件,然后使用 this
将事件分配给变量
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 中,剪贴板变量没有 "items" 或 "types"。我会上传那个图片服务器,但我没有得到 base64 编码的数据。
有可能...在受信任的站点上。
你看,IE的剪贴板数据is pretty well defined。它仅支持文本或 url。
WScript nor ActiveXObject 都没有提供更好的剪贴板访问。
但是你可以使用PowerShell to access .Net, including the Clipboard
, which has a nice little method GetImage()
。
通过 WSH, as is Base64 encoding.
调用 PowerShell 很简单
这只剩下如何检索提取的数据的问题。
通常您应该 use a file,因为我们已经在使用 ActiveX。
但出于演示目的,这里我将使用注册表。
这为我们省去了创建 FileSystemObject
和检测临时文件夹的麻烦。
下面的 html 将以 base64 格式抓取剪贴板上的任何图像,并将其放入 <img>
.
<!DOCTYPE html><meta charset="utf-8"><img width=500 /><script>
try {
var doc = document, body = doc.body, shell = new ActiveXObject('WScript.shell');
var key = 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer';
var cmd = "function Get-ClipImg {Add-Type -AssemblyName 'System.Windows.Forms';"+
"$s=New-Object System.IO.MemoryStream;"+
"[System.Windows.Forms.Clipboard]::GetImage().Save($s,[System.Drawing.Imaging.ImageFormat]::Png);"+
"[Microsoft.Win32.Registry]::SetValue('"+key+"','tmp_clipboard',[System.Convert]::ToBase64String($s.ToArray()))"+
"} Get-ClipImg";
shell.run( 'powershell -Command "'+cmd+'"', 0, true );
var data = shell.RegRead( key + '\tmp_clipboard' );
shell.RegDelete( key + '\tmp_clipboard' );
if ( ! data.trim() ) body.textContent = 'Clipboard has no image';
else doc.querySelector('img').src = 'data:image/png;base64,' + data;
} catch ( err ) { body.textContent = err; }
</script>
所以,这就是一种在 IE 中获取剪贴板图像的方法,无需使用 Flash 或 Java。
只要该站点是可信的。 (包括本地文件)
或者你可以use Flash or Java。
有可能...在任何网站上:) 但是,没有跨浏览器的方法。
在 Chrome 和 Opera(很可能是 Safari,但我现在无法测试)你可以访问您在问题中所写的剪贴板。事实上,这个方法只是 Chromium bag 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>
在 IE 和 Firefox 中,您可以使用不同的方法获得相同的结果。幸运的是,这些浏览器可以毫无问题地将打印屏幕粘贴到编辑器中,因此您根本不需要访问剪贴板。您只需监听粘贴事件并使用间隔捕获图像已创建但仍未呈现的时间点。然后您只需获取图像源并清空编辑器即可。
下面的代码实现了这个算法。当你在 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>
我搜索了很多但没有找到从剪贴板获取 base64 编码的数据。我可以捕获粘贴事件,然后使用 this
将事件分配给变量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 中,剪贴板变量没有 "items" 或 "types"。我会上传那个图片服务器,但我没有得到 base64 编码的数据。
有可能...在受信任的站点上。
你看,IE的剪贴板数据is pretty well defined。它仅支持文本或 url。 WScript nor ActiveXObject 都没有提供更好的剪贴板访问。
但是你可以使用PowerShell to access .Net, including the Clipboard
, which has a nice little method GetImage()
。
通过 WSH, as is Base64 encoding.
这只剩下如何检索提取的数据的问题。
通常您应该 use a file,因为我们已经在使用 ActiveX。
但出于演示目的,这里我将使用注册表。
这为我们省去了创建 FileSystemObject
和检测临时文件夹的麻烦。
下面的 html 将以 base64 格式抓取剪贴板上的任何图像,并将其放入 <img>
.
<!DOCTYPE html><meta charset="utf-8"><img width=500 /><script>
try {
var doc = document, body = doc.body, shell = new ActiveXObject('WScript.shell');
var key = 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer';
var cmd = "function Get-ClipImg {Add-Type -AssemblyName 'System.Windows.Forms';"+
"$s=New-Object System.IO.MemoryStream;"+
"[System.Windows.Forms.Clipboard]::GetImage().Save($s,[System.Drawing.Imaging.ImageFormat]::Png);"+
"[Microsoft.Win32.Registry]::SetValue('"+key+"','tmp_clipboard',[System.Convert]::ToBase64String($s.ToArray()))"+
"} Get-ClipImg";
shell.run( 'powershell -Command "'+cmd+'"', 0, true );
var data = shell.RegRead( key + '\tmp_clipboard' );
shell.RegDelete( key + '\tmp_clipboard' );
if ( ! data.trim() ) body.textContent = 'Clipboard has no image';
else doc.querySelector('img').src = 'data:image/png;base64,' + data;
} catch ( err ) { body.textContent = err; }
</script>
所以,这就是一种在 IE 中获取剪贴板图像的方法,无需使用 Flash 或 Java。 只要该站点是可信的。 (包括本地文件)
或者你可以use Flash or Java。
有可能...在任何网站上:) 但是,没有跨浏览器的方法。
在 Chrome 和 Opera(很可能是 Safari,但我现在无法测试)你可以访问您在问题中所写的剪贴板。事实上,这个方法只是 Chromium bag 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>
在 IE 和 Firefox 中,您可以使用不同的方法获得相同的结果。幸运的是,这些浏览器可以毫无问题地将打印屏幕粘贴到编辑器中,因此您根本不需要访问剪贴板。您只需监听粘贴事件并使用间隔捕获图像已创建但仍未呈现的时间点。然后您只需获取图像源并清空编辑器即可。
下面的代码实现了这个算法。当你在 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>