将图像复制到剪贴板 [英] Copy Image to Clipboard

查看:146
本文介绍了将图像复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像作为DataURL字符串。

我想以编程方式将此图像复制到ClipBoard。



我找到了两个函数,但它们都不起作用。
虽然第一个函数在复制文本时效果很好 - 复制(Hello!,text);

PS我有剪贴板写权限

第一:

 函数copy(str,mimetype){ 
document.oncopy = function(event){
event.clipboardData.setData(mimetype,str);
event.preventDefault();
};
document.execCommand(Copy,false,null);
}

第二:

 函数copyImage(url){
var img = document.createElement('img');
img.src = url;
document.body.appendChild(img);
var r = document.createRange();
r.setStartBefore(img);
r.setEndAfter(img);
r.selectNode(img);
var sel = window.getSelection();
sel.addRange(r);
document.execCommand('Copy');


解决方案

看起来这是不可能的。自2012年以来,Chrome一直存在一个阻止它的错误! https://bugs.chromium.org/p/chromium/issues/ detail?id = 150835

I have an image as a DataURL string.
And I want to copy this image programmatically to ClipBoard.

I found two functions, but neither of them works. Though, first function works well when you copy text - copy("Hello!","text");

PS I have a "clipboardWrite" permission.

First:

function copy(str, mimetype) {
    document.oncopy = function(event) {
        event.clipboardData.setData(mimetype, str);
        event.preventDefault();
    };
    document.execCommand("Copy", false, null);
}

Second:

function copyImage(url){
    var img=document.createElement('img');
    img.src=url;
    document.body.appendChild(img);
    var r = document.createRange();
    r.setStartBefore(img);
    r.setEndAfter(img);
    r.selectNode(img);
    var sel = window.getSelection();
    sel.addRange(r);
    document.execCommand('Copy');
}

解决方案

Seems this isn't possible. There has been a bug preventing it in Chrome since 2012! https://bugs.chromium.org/p/chromium/issues/detail?id=150835

这篇关于将图像复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆