简体   繁体   English

有没有办法使用没有库的纯 javascript 将图像复制到剪贴板?

[英]Is there any way to copy image to clipboard with pure javascript without libraries?

I've tried using document.execCommand('copy') like this site but it didn't work (nothing got copied to my clipboard despite the fact that the console.log said it was successful).我试过像这个网站一样使用 document.execCommand('copy') 但它没有用(尽管 console.log 说它成功,但没有任何东西被复制到我的剪贴板)。 I also used the navigator.clipboard API but that didn't work for my jpg images, and here is its code:我还使用了 navigator.clipboard API 但这对我的 jpg 图像不起作用,这是它的代码:

navigator.clipboard.write(
[
    new ClipboardItem({
        'image/jpeg': new Blob( ['media/anime_0.jpg'],{type:'image/jpeg'} )
    })
])
.then(e=>{console.log('Copied to clipboard')})
.catch(e=>{console.log(e)})

The above code produces the following error:上面的代码产生以下错误:

DOMException: Sanitized MIME type image/jpeg not supported on write.

Anyone know if I'm doing anything wrong or if it's even possible to copy images to clipboard without using external libraries?任何人都知道我是否做错了什么,或者甚至可以在不使用外部库的情况下将图像复制到剪贴板?

Thanks Keith for the link to: convert image into blob using javascript感谢 Keith 提供的链接: convert image into blob using javascript

This is the solution I used for my app (it will only save images as png, as jpeg/jpg files keep giving me the DOMException error.这是我用于我的应用程序的解决方案(它只会将图像保存为 png,因为 jpeg/jpg 文件不断给我 DOMException 错误。

const img = new Image
const c = document.createElement('canvas')
const ctx = c.getContext('2d')

function setCanvasImage(path,func){
    img.onload = function(){
        c.width = this.naturalWidth
        c.height = this.naturalHeight
        ctx.drawImage(this,0,0)
        c.toBlob(blob=>{
            func(blob)
        },'image/png')
    }
    img.src = path
}

setCanvasImage('media/anime_0.jpg',(imgBlob)=>{
    console.log('doing it!')
    navigator.clipboard.write(
        [
            new ClipboardItem({'image/png': imgBlob})
        ]
    )
    .then(e=>{console.log('Image copied to clipboard')})
    .catch(e=>{console.log(e)})
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 PURE javascript 中复制到剪贴板的方法? - Way to copy to clipboard in the PURE javascript? 如何在没有外部库的情况下从 Javascript 书签复制到剪贴板? - How to copy to the clipboard from a Javascript bookmarklet without external libraries? 是否可以使用HTML,JavaScript和ActionScript在不使用zClip的情况下创建“复制到剪贴板”按钮? - Any way to use HTML, JavaScript, and ActionScript to create a Copy to Clipboard button WITHOUT using zClip? 复制到Mozzila FireFox中的剪贴板纯JavaScript函数 - Copy to clipboard pure javascript function in mozzila firefox Javascript 将文本复制到剪贴板,无需单击页面加载上的任何按钮 - Javascript copy text to clipboard without click any button on page load 有没有办法仅使用javascript命令选择图像并将其复制到剪贴板? - Is there a way to select and copy an image to clipboard only with javascript commands? 使用 JavaScript 将图像复制到剪贴板 - Copy image to clipboard using JavaScript 选择文本并在纯JavaScript中聚焦特定文本区域时将其复制到剪贴板? - Select the text and copy to clipboard when focused specific textarea in pure JavaScript? 将数据复制到剪贴板而不选择任何文本 - Copy data to clipboard without selecting any text 将文本复制到剪贴板而不使用任何输入 - Copy the text to the Clipboard without using any input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM