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

199
Views
Drop event not getting file list

I want to drag an image from browser and drop it inside an object and get the dropped file simply like this:

$('#image-upload-preview').on('drop', drop);

function drop(e) {
  const file = e.originalEvent.dataTransfer;
  console.log('file', file[0]); // this brings undefined
  //console.log('file', file); // this bring empty file list
}

window.addEventListener("dragover", (e) => {
  e = e || event;
  e.preventDefault();
}, false);

window.addEventListener("drop", (e) => {
  e = e || event;
  e.preventDefault();
}, false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="image_container">
  <img id="image-upload-preview" style="width:100%" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSClDjLZzrjD5FHPX-nNU5QoTWK-C2XcOD-M6Aabozw6Oem8-0ejje8Lk5DmNbhVOvvMfc&usqp=CAU">
  <div id="cropped_result"></div>
</div>

But the file returns empty file list???

How can I fix this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You dont get list of files because your const is not correct.

You need to put .files : e.originalEvent.dataTransfer.const.files

$('#image-upload-preview').on('drop', drop);

function drop(e) {
  e.stopPropagation();
  e.preventDefault();

  var file = e.originalEvent.dataTransfer.files;
  var fd = new FormData();

  console.log(file[0]);

  fd.append('file', file[0]);
  
  // process upload with ajax
}

window.addEventListener("dragover", (e) => {
  e = e || event;
  e.preventDefault();
}, false);

window.addEventListener("drop", (e) => {
  e = e || event;
  e.preventDefault();
}, false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="image_container">
  <img id="image-upload-preview" style="width:100%" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSClDjLZzrjD5FHPX-nNU5QoTWK-C2XcOD-M6Aabozw6Oem8-0ejje8Lk5DmNbhVOvvMfc&usqp=CAU">
  <div id="cropped_result"></div>
</div>

about 4 years ago · Juan Pablo Isaza 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!