วิธีวางไฟล์

การวางไฟล์ลงในเบราว์เซอร์ประกอบด้วยการใช้เหตุการณ์ paste ของ HTMLElement

การใช้เหตุการณ์ paste ของ HTMLElement

ขั้นตอนแรกคือการเพิ่ม Listener เหตุการณ์สำหรับเหตุการณ์ paste ที่องค์ประกอบที่ต้องการ ซึ่งโดยทั่วไปจะอยู่ที่ระดับ document จึงไม่จำเป็นต้องโฟกัสองค์ประกอบที่เฉพาะเจาะจง จากนั้นให้ใช้ Clipboard API ซึ่งจะให้สิทธิ์เข้าถึง ช่อง clipboardData ของเหตุการณ์ paste ของ HTMLElement's ซึ่งคุณสามารถวนซ้ำในรายการ files ได้ คุณสามารถตัดสินใจว่าจะแสดงไฟล์แต่ละไฟล์ที่วางลงบนหน้าจอหรือไม่ โดยพิจารณาจากประเภท MIME ของไฟล์ เช่น ในกรณีของรูปภาพหรือวิดีโอ หรือจะวางเนื้อหาข้อความของไฟล์ลงในองค์ประกอบ textarea ในกรณีของไฟล์ข้อความก็ได้

Browser Support

  • Chrome: 66.
  • Edge: 79.
  • Firefox: 63.
  • Safari: 13.1.

Source

document.addEventListener('paste', async (e) => {
  // Prevent the default behavior, so you can code your own logic.
  e.preventDefault();
  if (!e.clipboardData.files.length) {
    return;
  }
  // Iterate over all pasted files.
  Array.from(e.clipboardData.files).forEach(async (file) => {
    // Add more checks here for MIME types you're interested in,
    // such as `application/pdf`, `video/mp4`, etc.
    if (file.type.startsWith('image/')) {
      // For images, create an image and append it to the `body`.
      const img = document.createElement('img');
      const blob = URL.createObjectURL(file);
      img.src = blob;
      document.body.append(img);
    } else if (file.type.startsWith('text/')) {
      // For text files, read the contents and output it into a `textarea`.
      const textarea = document.createElement('textarea');
      textarea.value = await file.text();
      document.body.append(textarea);
    }
  });
});

อ่านเพิ่มเติม

สาธิต

HTML

<!DOCTYPE html>
<html>
  <head>
    <title>How to paste files</title>
  </head>
  <body>
    <h1>How to paste files</h1>
    <p>Hit <kbd>⌘</kbd> + <kbd>v</kbd> (for macOS) or <kbd>ctrl</kbd> + <kbd>v</kbd>
      (for other operating systems) to paste image or text file(s) anywhere in this page.
    </p>
  </body>
</html>

CSS


        html {
  box-sizing: border-box;
  font-family: system-ui, sans-serif;
  color-scheme: dark light;
}

*, *:before, *:after {
  box-sizing: inherit;
}

body {
  margin: 1rem;
}

img {
  height: auto;
  max-width: 100%;
  display: block;
}
        

JS


        document.addEventListener('paste', async (e) => {
  // Prevent the default behavior, so you can code your own logic.
  e.preventDefault();
  if (!e.clipboardData.files.length) {
    return;
  }
  // Iterate over all pasted files.
  Array.from(e.clipboardData.files).forEach(async (file) => {
    // Add more checks here for MIME types you're interested in,
    // such as `application/pdf`, `video/mp4`, etc.
    if (file.type.startsWith('image/')) {
      // For images, create an image and append it to the `body`.
      const img = document.createElement('img');
      const blob = URL.createObjectURL(file);
      img.src = blob;
      document.body.append(img);
    } else if (file.type.startsWith('text/')) {
      // For text files, read the contents and output it into a `textarea`.
      const textarea = document.createElement('textarea');
      textarea.value = await file.text();
      document.body.append(textarea);
    }
  });
});