{"id":11015,"date":"2023-09-17T16:06:56","date_gmt":"2023-09-17T08:06:56","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11015"},"modified":"2023-09-17T16:06:56","modified_gmt":"2023-09-17T08:06:56","slug":"js-copy-image-clipboard","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2023\/09\/js-copy-image-clipboard\/","title":{"rendered":"\u53c8get\u5230\u4e86\uff0cJS\u590d\u5236\u56fe\u7247\u5230\u526a\u5207\u677f"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11015\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11015<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202309-img-copy.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202309\/copyimg-cover.jpg\" width=\"435\" height=\"280\" alt=\"\u5c01\u9762\u56fe \u9cd0\u9c7c\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u603b\u8981\u6709\u539f\u56e0\u7684<\/h3>\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u7a81\u7136\u60f3\u5230\u4e86\u89e3\u5982\u4f55\u4f7f\u7528JS\u590d\u5236\u56fe\u7247\u5230\u526a\u5207\u677f\u5462\uff1f<\/p>\n<p>\u56e0\u4e3a\u4e0a\u5468\u5f04\u4e86\u4e2a<a href=\"https:\/\/www.zhangxinxu.com\/sp\/tinyimg\/\" rel=\"noopener\" target=\"_blank\">PNG\/JP\u5728\u7ebf\u538b\u7f29\u7684\u5de5\u5177<\/a>\uff0c\u4e00\u5f00\u59cb\u7684\u65f6\u5019\uff0c\u56fe\u7247\u83b7\u53d6\u7684\u9014\u5f84\u53ea\u80fd\u662f\u4e0b\u8f7d\u5230\u672c\u5730\u3002<\/p>\n<p>\u4f46\u5f88\u591a\u65f6\u5019\uff0c\u6211\u662f\u5e0c\u671b\u8fd9\u79cd\u538b\u7f29\u597d\u7684\u56fe\u7247\u53ef\u4ee5\u76f4\u63a5\u4e0a\u4f20\uff0c\u4e0d\u9700\u8981\u518d\u4ece\u672c\u5730\u8f6c\u4e00\u5708\u3002<\/p>\n<p>\u6b64\u65f6\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u590d\u5236\u529f\u80fd\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u76f4\u63a5\u70b9\u51fb\u4e00\u4e2a\u6309\u94ae\uff0c\u538b\u7f29\u597d\u7684\u56fe\u7247\u590d\u5236\u5230\u526a\u5207\u677f\uff0c\u7136\u540e\u518d\u53bb\u4e0a\u4f20\u9875\u9762\u8fdb\u884c\u4e0a\u4f20\uff0c\u591a\u65b9\u4fbf\uff0c\u591a\u8f7b\u677e\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5c31\u53bb\u4e86\u89e3\u4e86\u4e0b\uff0c\u8fd8\u662f\u5b66\u5230\u4e86\u4e0d\u5c11\u4e1c\u897f\u7684\u3002<\/p>\n<p>\u8fd9\u91cc\u548c\u5927\u5bb6\u5206\u4eab\u4e0b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5148\u4ece\u7b80\u5355\u7684\u5f00\u59cb\u3002<\/p>\n<h3>\u4e8c\u3001\u539f\u751f\u7684\u56fe\u7247\u590d\u5236API<\/h3>\n<p>\u6d4f\u89c8\u5668\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a ClipboardItem \u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u53ef\u4ee5\u6784\u9020\u6784\u9020\u526a\u5207\u677f\u590d\u5236\u5bf9\u8c61\u3002<\/p>\n<p>\u4f8b\u5982\u4e00\u6bb5\u6587\u672c\u7684\u590d\u5236\uff08\u4ee3\u7801\u53d6\u81ea<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Clipboard\/write\" rel=\"noopener\" target=\"_blank\">MDN\u6587\u6863<\/a>\uff09\uff1a<\/p>\n<pre>function setClipboard(text) {\r\n  const type = \"text\/plain\";\r\n  const blob = new Blob([text], { type });\r\n  const data = [new ClipboardItem({ [type]: blob })];\r\n\r\n  navigator.clipboard.write(data).then(() =&gt; {\r\n    \/* success *\/\r\n  }, () =&gt; {\r\n    \/* failure *\/\r\n  });\r\n}<\/pre>\n<p>\u56fe\u7247\u4e5f\u4e0d\u5728\u8bdd\u4e0b\u3002<\/p>\n<p>\u6211\u4e13\u95e8\u5199\u4e86\u4e2a\u590d\u5236\u56fe\u7247\u7684\u65b9\u6cd5\uff0c\u53c2\u6570\u5c31\u662f\u9875\u9762\u4e2d\u7684IMG\u5143\u7d20\u5bf9\u8c61\u3002<\/p>\n<pre>const doCopyImg2Clipboard = async (image, success = () =&gt; {}, failure = () =&gt; {}) =&gt; {\r\n    if (!image || !image.src) {\r\n        return;    \r\n    }\r\n    \r\n    \/\/ \u9700\u8981\u590d\u5236\u7684\u56fe\u7247\u7684\u5730\u5740\r\n    const src = image.src;\r\n    \/\/ \u8bf7\u6c42\u8be5\u5730\u5740\uff0c\u8fd4\u56de\u56fe\u7247\u7684blob\u6570\u636e\r\n    const response = await fetch(src);\r\n    \r\n    \/\/ \u9700\u8981\u662fblob\u6570\u636e\u683c\u5f0f\r\n    const blob = response.blob();\r\n    \r\n    \/\/ \u4f7f\u7528\u526a\u5207\u677fAPI\u8fdb\u884c\u590d\u5236\r\n    const data = [new ClipboardItem({\r\n        [blob.type || 'image\/' + src.split('.').slice(-1)[0].replace('jpg', 'jpeg')]: blob    \r\n    })];\r\n    \r\n    navigator.clipboard.write(data).then(success, failure);\r\n}<\/pre>\n<p>\u5982\u679c\u4f60\u5df2\u7ecf\u6709\u4e86\u56fe\u7247\u7684\u6570\u636e\uff08\u4f8b\u5982\u672c\u5730\u4e0a\u4f20\u3001\u62d6\u62fd\u6216\u7c98\u8d34\u7684\u56fe\u7247\uff09\uff0c\u5219fetch\u6b65\u9aa4\u53ef\u4ee5\u7565\u8fc7\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5f53\u9875\u9762\u4e2d\u6709\u5982\u4e0b\u6240\u793a\u7684HTML\u4ee3\u7801\uff08\u4e00\u5f20\u56fe\uff0c\u4e00\u4e2a\u6309\u94ae\uff09\uff1a<\/p>\n<pre>&lt;button id=\"button\" type=\"primary\"&gt;\u590d\u5236\u56fe\u7247&lt;\/button&gt;\r\n&lt;img id=\"image\" src=\".\/mybook.jpg\"&gt;<\/pre>\n<p>\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u89e6\u53d1\u56fe\u7247\u7684\u590d\u5236\u4e86\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u70b9\u51fb\u6309\u94ae\u8fdb\u884c\u590d\u5236\r\nbutton.addEventListener('click', () =&gt; {\r\n    doCopyImg2Clipboard(image, function () {\r\n        new LightTip('\u590d\u5236\u6210\u529f', 'success');\r\n    }, function (err) {\r\n        new LightTip('\u590d\u5236\u5931\u8d25\uff1a' + err, 'error');\r\n    });\r\n});<\/pre>\n<p>\u7136\u800c\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u5f97\u5230\u7684\u7ed3\u679c\u5374\u662f\u5931\u8d25\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202309\/2023-09-16_231823.png\" width=\"470\" height=\"177\" alt=\"\u590d\u5236\u5931\u8d25\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u9519\u8bef\u63d0\u793a\u53ef\u4ee5\u770b\u51fa\uff0c\u6d4f\u89c8\u5668\u662f\u4e0d\u652f\u6301 jpg \u683c\u5f0f\u7684\u56fe\u7247\u4fe1\u606f\u5199\u5165\u526a\u5207\u677f\u7684\u3002<\/p>\n<h4>\u8bd5\u8bd5PNG\u5462\uff1f<\/h4>\n<p>\u5982\u679c\u6211\u4eec\u628a\u56fe\u7247\u7684JPG\u683c\u5f0f\uff0c\u8f6c\u6362\u4e3aPNG\u683c\u5f0f\uff0c\u518d\u8bd5\u4e00\u8bd5\uff0c\u6bd4\u65b9\u8bf4\uff1a<\/p>\n<pre>&lt;button id=\"button\" type=\"primary\"&gt;\u590d\u5236\u56fe\u7247&lt;\/button&gt;\r\n&lt;img id=\"image\" src=\".\/mybook.png\"&gt;<\/pre>\n<p>\u5219\u5c31\u53ef\u4ee5\u6210\u529f\u590d\u5236\uff01<\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202309\/js-copy-image-to-clipboard-demo.php\" rel=\"noopener\" target=\"_blank\">\u590d\u5236JPG\u548cPNG\u56fe\u7247\u5230\u526a\u5207\u677f\u662f\u5426\u6210\u529fdemo<\/a><\/p>\n<p>\u6bd4\u65b9\u8bf4\uff0c\u6211\u4eec\u70b9\u51fbdemo\u53f3\u4fa7\u7684\u90a3\u4e2a\u6309\u94ae\uff0c\u6b64\u65f6\uff0c\u63d0\u793a\u7684\u5c31\u4f1a\u662f\u590d\u5236\u6210\u529f\uff0c\u5982\u4e0b\u622a\u56fe\u793a\u610f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202309\/2023-09-16_232414.png\" width=\"544\" height=\"281\" alt=\"\u590d\u5236\u6210\u529f\u63d0\u793a\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6b64\u65f6\uff0cPNG\u56fe\u7247\u4fe1\u606f\u5c31\u5df2\u7ecf\u5728\u526a\u5207\u677f\u4e2d\u4e86\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u627e\u4e2a\u5bcc\u6587\u672c\u8f93\u5165\u6846\uff0c\u6216\u8005\u5728\u7ebf\u6587\u6863\u7f16\u8f91\u5668\u7b49\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n<p>\u4f8b\u5982demo\u9875\u9762\u63d0\u4f9b\u7684\u8f93\u5165\u6846\uff0c\u6211\u4eec\u6267\u884c\u4e0bCtrl+V\uff0c\u5219\u53ef\u4ee5\u770b\u5230\u56fe\u7247\u51fa\u73b0\u4e86\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202309\/2023-09-16_232436.png\" width=\"467\" height=\"356\" alt=\"\u590d\u5236\u7c98\u8d34\u5230\u8f93\u5165\u6846\" class=\"alignnone size-medium\" \/><\/p>\n<p>OK\uff0c\u597d\uff0c\u4e0b\u9762\u95ee\u9898\u6765\u4e86\uff0c\u9875\u9762\u4e2d\u6709\u4e9b\u56fe\u7247\u4ed6\u5c31\u662fJPG\u683c\u5f0f\u7684\uff0c\u6211\u975e\u8981\u590d\u5236\uff0c\u6709\u6ca1\u6709\u4ec0\u4e48\u529e\u6cd5\u5462\uff1f<\/p>\n<h3>\u4e09\u3001JPG\/WebP\u56fe\u7247\u7684\u89e3\u51b3\u4e4b\u9053<\/h3>\n<p>\u8981\u60f3\u8ba9JPG\u6216\u8005webp\u683c\u5f0f\u7684\u56fe\u7247\u4e5f\u652f\u6301\u526a\u5207\u677f\u590d\u5236\uff0c\u5f88\u7b80\u5355\uff0c\u8f6c\u4e3aPNG\u65e0\u635f\u56fe\u5c31\u597d\u4e86\u5440\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u7ed8\u5236\u5728canvas\u4e0a\uff0c\u518d\u4f7f\u7528canvas\u7684toBlob()\u65b9\u6cd5\u8f6c\u4e00\u4e0b\u5c31\u5662\u5566\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u4e0a\u9762\u63d0\u4f9b\u7684\u590d\u5236\u4ee3\u7801\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u4e0b\u3002<\/p>\n<p>\u5b8c\u6574JS\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre>const doCopyImg2Clipboard = async (image, success = () =&gt; {}, failure = () =&gt; {}) =&gt; {\r\n    if (!image || !image.src) {\r\n        return;    \r\n    }\r\n    \r\n    \/\/ \u539f\u56fe\u5c3a\u5bf8\r\n    const { naturalWidth, naturalHeight } = image;\r\n    \r\n    if (!naturalWidth) {\r\n        failure('\u56fe\u7247\u5c1a\u672a\u6210\u529f\u52a0\u8f7d');\r\n        \r\n        return;\r\n    }\r\n    \r\n    \/\/ \u7ed8\u5236\u56fe\u7247\u5230canvas\u4e0a\r\n    const canvas = document.createElement('canvas');\r\n    canvas.width = naturalWidth;\r\n    canvas.height = naturalHeight;\r\n    \/\/ canvas\u7ed8\u5236\u4e0a\u4e0b\u6587\r\n    const context = canvas.getContext('2d');\r\n    \/\/ \u56fe\u7247\u7ed8\u5236\r\n    context.drawImage(image, 0, 0, naturalWidth, naturalHeight);\r\n    \/\/ \u8f6c\u4e3aBlob\u6570\u636e\r\n    canvas.toBlob(blob =&gt; {\r\n        \/\/ \u4f7f\u7528\u526a\u5207\u677fAPI\u8fdb\u884c\u590d\u5236\r\n        const data = [new ClipboardItem({\r\n            ['image\/png']: blob    \r\n        })];\r\n        \r\n        navigator.clipboard.write(data).then(success, failure);\r\n    });\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u65e0\u8bba\u662fJPG\u56fe\u7247\u8fd8\u662fWebP\u56fe\u7247\u90fd\u53ef\u4ee5\u590d\u5236\u5230\u526a\u5207\u677f\u4e86\u3002<\/p>\n<pre>doCopyImg2Clipboard(image, function () {\r\n    console.log('\u590d\u5236\u6210\u529f');\r\n});<\/pre>\n<p>\u6709\u6f14\u793a\u9875\u9762\u53ef\u4ee5\u4f53\u9a8c\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202309\/js-copy-jpg-to-clipboard-demo.php\" rel=\"noopener\" target=\"_blank\">JS\u590d\u5236JPG\u56fe\u7247\u5230\u526a\u5207\u677fdemo<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202309\/2023-09-17_120741.png\" width=\"626\" height=\"593\" alt=\"\u590d\u5236JPG\u56fe\u7247\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u7136\u800c\uff0c\u867d\u7136\u56fe\u7247\u590d\u5236\u4e86\uff0c\u4f46\u662f\u53d8\u6210PNG\u4e4b\u540e\uff0c\u539f\u672c\u5c3a\u5bf8\u9002\u5408\u7684JPG\u56fe\u7247\u5c3a\u5bf8\u5c31\u4f1a\u53d8\u5f97\u5f88\u5927\u3002<\/p>\n<p>\u90a3\u56fe\u7247\u7b49\u4e8e\u767d\u538b\u7f29\u4e86\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5982\u679c\u5e0c\u671b\u590d\u5236JPG\u56fe\u7247\u7684\u65f6\u5019\uff0c\u4fdd\u7559\u8be5JPG\u539f\u59cb\u7684\u5c3a\u5bf8\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f<\/p>\n<h3>\u56db\u3001\u82e5\u60f3\u8981\u4fdd\u7559\u539f\u56fe\u5c3a\u5bf8\uff1f<\/h3>\n<p>\u89e3\u51b3\u65b9\u6cd5\u662f\u4e0d\u662f\u590d\u5236blob\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u800c\u662f\u590d\u5236\u8868\u793a\u56fe\u7247\u6570\u636e\u7684base64\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u7136\u540e\u5728\u4e0a\u4f20\u6216\u8005\u7c98\u8d34\u7684\u5730\u65b9\u518d\u4e8c\u6b21\u5904\u7406\u3002<\/p>\n<h4>\u56fe\u7247\u8f6cbase64\u5e76\u590d\u5236\u5230\u526a\u5207\u677f<\/h4>\n<p>\u4e0d\u54d4\u54d4\uff0c\u76f4\u63a5\u770b\u4ee3\u7801\uff0c\u4f7f\u7528FileReader\u5bf9\u8c61\u5c06blob\u6570\u636e\u8f6c\u4e3abase64\u3002<\/p>\n<pre>const doCopyImgBase64Clipboard = (image, success = () =&gt; {}, failure = () =&gt; {}) =&gt; {\r\n    if (!image || !image.src) {\r\n        return;    \r\n    }\r\n    \r\n    \/\/ \u9700\u8981\u590d\u5236\u7684\u56fe\u7247\u7684\u5730\u5740\r\n    const src = image.src;\r\n    \/\/ \u8bf7\u6c42\u8be5\u5730\u5740\uff0c\u8fd4\u56de\u56fe\u7247\u7684blob\u6570\u636e\r\n    fetch(src).then(response =&gt; response.blob()).then(blob =&gt; {\r\n        \/\/ blob\u6570\u636e\u8f6cbase64\r\n        const reader = new FileReader();\r\n        reader.onload = function() {\r\n            navigator.clipboard.writeText(this.result).then(success, failure);\r\n        };\r\n        reader.readAsDataURL(blob) ;\r\n    });\r\n}<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<dl>\n<dt><strong>image<\/strong><\/dt>\n<dd>\u8868\u793a\u9875\u9762\u4e2d\u7684IMG\u5143\u7d20\u5bf9\u8c61\u3002<\/dd>\n<dt><strong>success<\/strong><\/dt>\n<dd>\u8868\u793a\u6210\u529f\u7684\u56de\u8c03<\/dd>\n<dt><strong>failure<\/strong><\/dt>\n<dd>\u8868\u793a\u5931\u8d25\u7684\u56de\u8c03<\/dd>\n<\/dl>\n<p>\u6b64\u65f6\uff0c\u5c31\u53ef\u4ee5\u70b9\u51fb\u6309\u94ae\uff0c\u4e00\u952e\u590d\u5236\u3002<\/p>\n<h4>base64\u56fe\u7247\u7684\u5904\u7406<\/h4>\n<p>\u5728\u8fd9\u573a\u573a\u666f\u4e0b\uff0c\u590d\u5236\u5e76\u4e0d\u662f\u6700\u96be\u7684\uff0c\u56e0\u4e3a\u6587\u672c\u590d\u5236\u6709\u7740\u975e\u5e38\u6210\u719f\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6bd4\u65b9\u8bf4\u6211\u4e4b\u524d\u5f04\u4e86\u4e2a<a href=\"https:\/\/gitee.com\/zhangxinxu\/copy-text\" rel=\"noopener\" target=\"_blank\">\u201c\u6781\u7b80\u6587\u5b57\u5185\u5bb9\u526a\u5207\u677f\u590d\u5236\u201d\u7684gitee\u9879\u76ee<\/a>\uff0c\u4f18\u70b9\u662f\u81ea\u5e26\u590d\u5236\u6210\u529f\u7684\u4ea4\u4e92\u63d0\u793a\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e1a\u754c\u77e5\u540d\u7684clipboard.js\uff1a<a href=\"https:\/\/github.com\/zenorocha\/clipboard.js\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/zenorocha\/clipboard.js<\/a><\/p>\n<p>\u96be\u70b9\u5728\u4e8e\u7c98\u8d34\u65f6\u5019\u5bf9base64\u6587\u672c\u7684\u5904\u7406\uff0c\u56e0\u4e3a\u5904\u7406\u7c98\u8d34\u6587\u672c\u5e76\u4e0d\u662f\u4e00\u4e2a\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\u573a\u666f\uff0c\u5927\u591a\u6570\u7684\u524d\u7aef\u90fd\u7f3a\u4e4f\u76f8\u5e94\u7684\u5904\u7406\u7ecf\u9a8c\u3002<\/p>\n<p>\u8fd9\u91cc\uff0c\u901a\u8fc7\u4e24\u4e2a\u4f8b\u5b50\uff0c\u6f14\u793a\u9884\u89c8\u548c\u4e0a\u4f20\u7684\u5904\u7406\u3002<\/p>\n<p><strong>1. \u9884\u89c8<\/strong><\/p>\n<p>base64\u672c\u8eab\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u56fe\u7247\u7684URL\u5730\u5740\uff0c\u56e0\u6b64\uff0c\u9884\u89c8\u76f8\u5bf9\u7b80\u5355\uff0c\u53ea\u8981\u521b\u5efa\u4e2a\u56fe\u7247DOM\u5143\u7d20\uff0c\u7136\u540e\u63d2\u5165\u5230\u9875\u9762\u4e2d\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u90a3\u5982\u4f55\u83b7\u5f97\u526a\u5207\u677f\u4e2d\u7c98\u8d34\u7684\u5185\u5bb9\u5462\uff1f\u6709\u5bf9\u5e94\u7684API\u7684\uff0c\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>\/\/ \u5728\u8f93\u5165\u6846\u4e2d\u7c98\u8d34\r\ninput.addEventListener('paste', event =&gt; {\r\n    var paste = event.clipboardData?.getData('text') || '';\r\n});<\/pre>\n<pre>\/\/ \u5728\u9875\u9762\u4e2d\u7c98\u8d34\r\ndocument.addEventListener('paste', event =&gt; {\r\n    var paste = event.clipboardData?.getData('text') || '';\r\n});<\/pre>\n<p>\u6b64\u65f6\uff0c\u53ea\u8981\u5224\u65ad <code>paste<\/code> \u5185\u5bb9\u5339\u914d base64 \u56fe\u7247\u683c\u5f0f\u5373\u53ef\u3002<\/p>\n<p>\u4ee5\u5728\u8f93\u5165\u6846\u4e2d\u7c98\u8d34base64\u56fe\u7247\u4e3a\u4f8b\uff0c\u5b8c\u6574\u4ea4\u4e92JavaScript\u4ee3\u7801\u53c2\u89c1\uff1a<\/p>\n<pre>\/\/ \u7c98\u8d34\u5bf9base64\u5185\u5bb9\u7684\u76d1\u63a7\u5904\u7406\r\ninput.addEventListener('paste', event =&gt; {\r\n    var clipboardData = event.clipboardData;\r\n\r\n    var paste = clipboardData?.getData('text') || '';\r\n\r\n    if (!\/^data:image\\\/[a-z]+;base64,\/.test(paste)) {\r\n        return;\r\n    }\r\n    \r\n    event.preventDefault();\r\n    \r\n    \/\/ \u6211\u662f\u8303\u56f4\r\n    const selection = document.getSelection();\r\n    \/\/ \u6211\u662f\u9009\u533a\r\n    const range = selection.getRangeAt(0);\r\n    \/\/ \u5220\u9664\u9009\u533a\u5185\u5bb9\uff0c\r\n    range.deleteContents();\r\n    \/\/ \u5e76\u66ff\u6362\u6210\u56fe\u7247\r\n    const imgNode = document.createElement('img');\r\n    imgNode.src = paste;\r\n    range.insertNode(imgNode);\r\n    \/\/ \u5149\u6807\u5b9a\u4f4d\u5728\u56fe\u7247\u540e\u9762\r\n    range.setStartAfter(imgNode);\r\n});<\/pre>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202309\/js-copy-base64-to-clipboard-demo.php\" rel=\"noopener\" target=\"_blank\">JS\u590d\u5236\u56fe\u7247base64\u4fe1\u606f\u5230\u526a\u5207\u677fdemo<\/a><\/p>\n<p>\u70b9\u51fb\u6309\u94ae\uff0c\u4f1a\u590d\u5236\u56fe\u7247\u7684base64\u4fe1\u606f\uff0c\u6b64\u65f6\uff0c\u518d\u7c98\u8d34\u5230\u9875\u9762\u4e2d\u7684\u6d4b\u8bd5\u8f93\u5165\u6846\uff0c\u4f1a\u53d1\u73b0\uff0c\u663e\u793a\u7684\u4e0d\u662f\u5b57\u7b26\u4e32\u5185\u5bb9\uff0c\u800c\u662f\u56fe\u7247\uff0c\u5c31\u662f\u56e0\u4e3a\u6709\u4e0a\u9762\u8fd9\u6bb5 JS \u89e3\u6790\u4ee3\u7801\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202309\/2023-09-17_153742.png\" width=\"497\" height=\"451\" alt=\"\u64cd\u4f5c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p><strong>2. \u4e0a\u4f20<\/strong><\/p>\n<p>base64\u56fe\u7247\u5730\u5740\u4e0a\u4f20\u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6cd5\uff0c\u4e00\u662f\u540e\u7aef\u5224\u65ad\u5904\u7406\uff0c\u5373\uff0c\u5982\u679cpost\u7684\u662f\u4e8c\u8fdb\u5236\u6570\u636e\u6d41\uff0c\u5982\u4f55\u5904\u7406\uff0c\u5982\u679cpost\u7684\u662f\u5b57\u7b26\u4e32\u6570\u636e\u6d41\uff0c\u53c8\u8be5\u5982\u4f55\u5904\u7406\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u5728\u524d\u7aef\u5904\u7406\uff0c\u5c06\u526a\u5207\u677f\u4e2d\u7684base64\u5b57\u7b26\u4e32\u8f6c\u4e3ablob\u6587\u4ef6\u6570\u636e\u518d\u4e0a\u4f20\uff0c\u8fd9\u6837\uff0c\u540e\u7aef\u90a3\u8fb9\u8f7b\u677e\u70b9\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u610f\u4e86\u4e0b\u5982\u4f55\u5904\u7406\uff1a<\/p>\n<pre>\/\/ base64 \u8f6c blob\r\nconst b64toBlob = (b64Data, contentType='', sliceSize=512) =&amp;gt; {\r\n  const byteCharacters = atob(b64Data);\r\n  const byteArrays = [];\r\n\r\n  for (let offset = 0; offset &amp;lt; byteCharacters.length; offset += sliceSize) {\r\n    const slice = byteCharacters.slice(offset, offset + sliceSize);\r\n\r\n    const byteNumbers = new Array(slice.length);\r\n    for (let i = 0; i &amp;lt; slice.length; i++) {\r\n      byteNumbers[i] = slice.charCodeAt(i);\r\n    }\r\n\r\n    const byteArray = new Uint8Array(byteNumbers);\r\n    byteArrays.push(byteArray);\r\n  }\r\n\r\n  const blob = new Blob(byteArrays, {type: contentType});\r\n  return blob;\r\n}\r\n\r\n\/\/ \u7c98\u8d34base64\u4e0a\u4f20\r\ndocument.addEventListener('paste', function (event) {\r\n    var clipboardData = event.clipboardData;\r\n\r\n    var paste = clipboardData?.getData('text') || '';\r\n\r\n    if (!paste.startsWith('data:image')) {\r\n        return;\r\n    }\r\n\r\n    \/\/ \u6838\u5fc3\u6570\u636e\r\n    var base64 = paste.split('base64,')[1];\r\n    \/\/ \u7c7b\u578b\r\n    var type = paste.split(';base64')[0].replace('data:', '');\r\n    \/\/ \u83b7\u53d6 blob \u6570\u636e\r\n    var blob = b64toBlob(base64, type);\r\n    \/\/ \u8bbe\u7f6e\u4e2a\u6587\u4ef6\u540d\uff0c\u4e0d\u7136\u662fundefined\r\n    blob.name = new Date().toLocaleString().replaceAll('\/', '-').replace(\/\\s\/g, '_').replaceAll(':', '') + '.' + (type.split('\/')[1] || 'png');\r\n\r\n    \/\/ \u6b64\u65f6\u7684 blob \u5c31\u662f\u9700\u8981\u4e0a\u4f20\u7684\u6587\u4ef6\u6570\u636e\u4e86...\r\n});<\/pre>\n<p>\u4e8e\u662f\uff0c\u6574\u4e2a\u4e00\u5957\u6d41\u7a0b\u5c31\u53ef\u4ee5\u8d70\u901a\u4e86\u3002<\/p>\n<p>\u524d\u7aef\u538b\u7f29\u56fe\u7247\uff0cJS\u590d\u5236\u538b\u7f29\u540e\u7684base64\u5b57\u7b26\u4fe1\u606f\uff0c\u7136\u540eJS\u89e3\u6790base64\u5e76\u4e0a\u4f20\u3002<\/p>\n<h3>\u7ed3\u8bed<\/h3>\n<p>\u65f6\u4e0d\u65f6\u7ed9\u81ea\u5df1\u627e\u4e2a\u9700\u6c42\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u4e1a\u52a1\u5f00\u53d1\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5e73\u5e38\u9047\u4e0d\u5230\u7684\u573a\u666f\uff0c\u79ef\u7d2f\u4e0d\u4e00\u6837\u7684\u7ecf\u9a8c\u3002<\/p>\n<p>\u8981\u662f\u4e0b\u6b21\u4e1a\u52a1\u5f00\u53d1\u9047\u5230\u7c7b\u4f3c\u9700\u6c42\uff0c\u90a3\u5c31\u662f\u5206\u5206\u949f\u4e0a\u624b\uff0c\u5f00\u53d1\u6548\u7387\u9886\u5148\u4e00\u6b65\u3002<\/p>\n<p>\u8fd8\u80fd\u64b8\u4e00\u7bc7\u6587\u7ae0\uff0c\u5b8c\u6210\u81ea\u5df1\u7ed9\u81ea\u5df1\u5236\u5b9a\u7684\u5e73\u5747\u6bcf\u5468\u9700\u8981\u4e00\u4e2a\u6280\u672f\u6587\u7ae0\u8f93\u51fa\u7684KPI\u3002<\/p>\n<p>\u4e00\u52b3\u591a\u5f97\uff0c\u5c82\u4e0d\u7f8e\u54c9\u3002<\/p>\n<p>\u5662\u5566\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u5566\u3002<\/p>\n<p>\u5e0c\u671b\u91cc\u9762\u7684\u5185\u5bb9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u5de5\u4f5c\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u5982\u679c\u60a8\u89c9\u5f97\u4e0d\u9519\uff0c\u6b22\u8fce<button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u8f6c\u53d1<\/button>\uff0c\u70b9\u8d5e\u3002<\/p>\n<p>? ? ? ? ?<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11015\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11015<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202309\/copyimg-cover.jpg\" width=\"160\" height=\"103\" alt=\"\u5c01\u9762\u56fe \u9cd0\u9c7c\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u8d77\u521d\u4ee5\u4e3a\u53ea\u662f\u4e2a\u5c0f\u9700\u6c42\uff0c\u5b9e\u73b0JS\u590d\u5236\u56fe\u7247\u5230\u526a\u5207\u677f\uff0c\u76f4\u5230\u6df1\u5165\u4e86\u89e3\uff0c\u624d\u53d1\u73b0\uff0c\u539f\u6765\u6d89\u53ca\u5230\u90a3\u4e48\u591a\u77e5\u8bc6\uff0c\u771f\u662f\u4e0d\u865a\u6b64\u5b66\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[710,872,1306,1064,638,1303,10,221],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11015"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11015"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11015\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}