通过JavaScript将base64的图片复制到剪切板,粘贴到微信的方案

木木俞
  • 192
  1. 尝试clipboard.js
  2. 也尝试了以下方案

const selection = window.getSelection();

const range = document.createRange();

range.selectNodeContents(element);

selection.removeAllRanges();

selection.addRange(range);

都没有办法实现将base64图片通过js放到剪切板粘贴到微信,求哪位大神做过类似需求,是否有解决方案,多谢

回复
阅读 2.1k
2 个回答
Dorian敏
  • 2
新手上路,请多包涵

楼主可以试试下面的方法,我在IE11和Chrome下测试是可以成功复制的

<div>
    <img id="hidImag" src="" />
</div>
<input id="copy" type="button" style="cursor: pointer;" value="复制图片到剪切板">
<script type="text/javascript">
    copy.onclick = function () {
        try {
            const base64Data = 'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAAAsTAAALEwEAmpwYAAAIGElEQVRo3t2ZC1AU9x3HPeEOOB6KhOCUTnicIAeCUGxQOA64O+S4QxAxaUwjNpiaDirYWgKVIdqgghDACsgzgHBvIIpvzUyaaZxJk+YxZmzajM40sbF1akwmNiS148z299/977G3t7v34NCZ7sx3uJ27/e/n83/tf/8sWvR/eoggfhCxj+KPg8pcjMtfuEOxZUvQlo6bZ5/tnSXI9FDZgvNMN5Wf0Dk2l6dxnvodlUztyIP0Dca7azYY38sqmxgseHr8+V/stayqqKgNwkILIiMJiIxMKG68+mbF0VliE50uKuU4GzuplNHpmEspToq6h0gpHCXkhUYiSWOFTEKmiNSiydmsjZb252pH5bh1RL7uQsGS8PBUbeMHf/BUoJQpoEECfUSyZoyQMwRWqqftydBPvrn5hZF0X7eIXaJ433tvs+HdEdjwKggU9gB4PyFXDxPJaqOTQKIKRz1FZJZYRquqmpa5kBDV149K3BVFPwoRL12aoT9w4wZX7bsSSAaBZDUlIFdBK6jNnAIJqikyco3tK93WoSdxazjx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ewL8Nxtc+0jc5tow/hgrk2UmQ4N/4DPp/N0P1KyiVEy8AAAAASUVORK5CYII=';
            if (document.body.createControlRange) { //IE 11 需要有个img标签
                var controlRange;
                var imgs = document.getElementById('hidImag');
                imgs.onload = function () {
                    controlRange = document.body.createControlRange();
                    imgs.contentEditable = 'true';
                    controlRange.addElement(imgs);
                    try {
                        var successful = controlRange.execCommand('copy');
                        var msg = successful ? 'successful' : 'unsuccessful';
                        console.log('Copying text command was ' + msg);
                    } catch (err) {
                        console.log(err);
                    }
                }
                imgs.src = 'data:image/png;base64,' + base64Data;
            }
            else { //chrome
                const blobInput = convertBase64ToBlob(base64Data, 'image/png');
                const clipboardItemInput = new ClipboardItem({ 'image/png': blobInput });
                navigator.clipboard.write([clipboardItemInput]);
                console.log('success');
            }
        } catch (e) {
            console.log(e);
        }
    }

    function convertBase64ToBlob(base64, type) {
        var bytes = window.atob(base64);
        var ab = new ArrayBuffer(bytes.length);
        var ia = new Uint8Array(ab);
        for (var i = 0; i < bytes.length; i++) {
            ia[i] = bytes.charCodeAt(i);
        }
        return new Blob([ab], { type: type });
    }
</script>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏