Dateien einfügen

Harry Theodoulou
Harry Theodoulou

Beim Einfügen von Dateien in den Browser wird das paste-Ereignis von HTMLElement verwendet.

paste-Ereignis des HTMLElement wird verwendet

Fügen Sie zuerst einen Event-Listener für das paste-Ereignis beim gewünschten Element hinzu. In der Regel befindet sich dies auf document-Ebene, sodass kein bestimmtes Element hervorgehoben werden muss. Als Nächstes verwenden Sie die Clipboard API. Damit erhalten Sie Zugriff auf das Feld clipboardData des Ereignisses paste des HTMLElement-Ereignisses, dessen files-Liste Sie dann iterieren können. Je nach MIME-Typ der einzelnen eingefügten Dateien können Sie entscheiden, ob sie wie bei einem Bild oder Video auf dem Bildschirm gerendert oder bei einer Textdatei z. B. der Text der Datei in ein textarea-Element eingefügt werden soll.

Unterstützte Browser

  • 66
  • 79
  • 63
  • 13.1

Quelle

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);
    }
  });
});

Weitere Informationen

Demo

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);
    }
  });
});