jle*_*ijo 14 javascript flash clipboard coffeescript
草案中有一个用于标准化剪贴板事件的API,但目前尚未在任何浏览器中实现http://dev.w3.org/2006/webapi/clipops/
我正在使用zclip(基于zeroclipboard)将文本从restfule服务复制到系统剪贴板:
$('.copy-to-clipboard').zclip
path:'/ZeroClipboard.swf'
setHandCursor: true
copy: ->
ajaxReturn = $.ajax
type: 'GET'
async: false
url: '/resources/copy_to_clipboard/' + $(this).attr("class").match(/[0-9]+/)
return ajaxReturn.responseText
Run Code Online (Sandbox Code Playgroud)
这是coffeescript.
如果服务(/ resources/copy_to_clipboard /)提供文本,则将其复制到右侧.但如果它服务于DOCX文件,它不会直接复制到剪贴板.看看rails控制器:
def copy_to_clipboard
send_file @resource.resource_content.content.file.file, :type => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
end
Run Code Online (Sandbox Code Playgroud)
你有没有随时解决复制BINARY数据到剪贴板?如何 ?
谢谢
我阅读了剪贴板 API 和事件并编写了以下代码,这对我有用。唯一的问题是使用NULL (0x0)值。使用代码的方法是使用x所需的二进制值和调用document.execCommand('copy')函数设置数组,恭喜您的数据位于剪贴板中!
var x = [0x1b, 0x68, 101, 108, 108, 0x6f, 0x7, 0x8];
var button = document.getElementById("copy-button");
button.addEventListener("click", function() {
document.execCommand('copy');
}, false);
document.addEventListener('copy', function(e) {
var str = '';
x.forEach(function(d) {
str += String.fromCharCode(d)
})
//You can ignore setting third parameter value
e.clipboardData.setData('text', str, true);
console.info('data copied');
e.preventDefault();
});Run Code Online (Sandbox Code Playgroud)
<button type="button" id="copy-button">Copy to clipboard</button>Run Code Online (Sandbox Code Playgroud)
这是在 Notepad++ 中粘贴结果,Show All Characters菜单项打开:

我希望这有帮助 ;)