Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Paste multiple files from clipboard not working

So i noticed everytime i paste some file, if i have more than 1 file on my clipboard area it only pastes 1 file, the others my code just don't process.

Here is the snippet to reproduce, even if i have more than 1 file on clipboard it only inserts 1 item in the array. I'm trying to figure out what is happening. (use image files to test).

const inputFileToBase64 = file => new Promise((resolve, reject) => {
  const reader = new FileReader()
  reader.readAsDataURL(file)

  reader.onload = () => resolve(reader.result)
  reader.onerror = error => reject(error)
})

const getBase64Files = async (items) => {
  const base64Files = []  
  
  for (const item of items) {  
     const base64 = await inputFileToBase64(item.getAsFile())
     
     base64Files.push(base64)
  }
  
  return base64Files
}

document.addEventListener('paste', async (event) => { 
  console.log('pasted')
  const items = event.clipboardData.items
  const files = await getBase64Files(items)
  console.log(files)
})
PASTE HERE

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The DataTransferItemList items seems to be consumed as soon as the event loop allows it to; it doesn't wait for a FileReader to read it. So, by the time the first file is read, items is empty.

If you allow the task queue to do its stuff, even with a setTimeout of 0, you lose all files:

  const items = event.clipboardData.items
  await new Promise(resolve => setTimeout(resolve, 0)) //wait 0ms
  const files = await getBase64Files(items) //no items, no files

and that's also happening with the working version of getBase64Files below.

Regardless of the explanation (which one might improve upon), it is always a good idea to start all reading at the same time and await for all to complete:

const getBase64Files = async (items) => {
  const base64Files = []

  await Promise.allSettled(
    Array.from(items).map(
      item => inputFileToBase64(item.getAsFile()).then(
        file => base64Files.push(file)
      )
    )
  )
   
  return base64Files
}

and that works.

about 4 years ago · Juan Pablo Isaza Relatório

0

This is not your mistake. For some reason, the item list only contains 1 item in chrome. In Firefox, it seems to be 0. No matter how many files you copy.

You should probably use the Clipboard API instead. https://developer.mozilla.org/en-US/docs/Web/API/Clipboard

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda