paste.html 粘贴图片 代码备份

paste.html 粘贴图片 代码备份


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <img id="pasteImg" />

  <script>
    document.addEventListener('paste', async (e) => {
      const clipboardData = e.clipboardData;
      const items = clipboardData.items;

      for (const item of items) {
        if (item.type.startsWith('image/')) {
          const blob = await item.getAsFile();

          const reader = new FileReader();
          reader.readAsDataURL(blob);

          reader.onload = function () {
            const base64Data = reader.result;
            document.getElementById('pasteImg').src = base64Data;
          };
        }
      }
    });
  </script>
</body>

</html>
posted @ 2025-02-18 13:44  彭成刚  阅读(19)  评论(0)    收藏  举报