Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Drop or Paste file on the Textarea

I want to make a message input like this. enter image description here

But I have a question how to get file data, which I drag & drop or Ctrl+V on the textarea to upload. I am using Riot.js and Symfony for this.

Here is my uncompleted code:

<form method="post" enctype="multipart/form-data" onsubmit="{ handleFileSubmit }">
   <textarea class="form-control" id="message" onpaste="{ handleFilePaste }" required> 
   </textarea>
</form>

handleFilePaste(e) {
  e.preventDefault();
  console.log(e.clipboardData.items);
  console.log(window.navigator.clipboard);
}

But the above console.log() didn't get any file data when I copy a file on my local drive and paste it on the textarea. If someone knows how to do it or have a good sample link, please share with me.

If you can, please share the drag & drop sample(important: the input field must be textarea).

I hope good replies. Thank you.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

When I had to do something similar in the past I didn't attach the eventlistener to the form itself, but to document as shown in the example below (which you can execute and paste something, it works), technically I'm not sure if it works the same on a textarea but it does work on document, it's not that important where you listen for the paste event as you are then deciding what you do when you detect a file being pasted.

function handleFilePaste(e) {

  const items = (e.clipboardData || e.originalEvent.clipboardData).items;
  console.log(items);
  
  for (let index in items) {
    const item = items[index];
    if ( item.kind === 'file' ) {
      return item.getAsFile();
    }
  }
}

document.addEventListener('DOMContentLoaded', () => {
  document.onpaste = function(e){
    const file = handleFilePaste(e);
    console.log(file);
    
    if ( !file ) { return; }
  }
});
<form method="post" enctype="multipart/form-data" onsubmit="{ handleFileSubmit }">
   <textarea class="form-control" id="message" placeholder="Paste something" required></textarea>
</form>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!