Search code examples
javascriptpapaparse

Papaparse not parsing the last file


I have a csv file with error. I want to show the error and let user select the file again and then parse it again.

But when i am re-uploading same file. It is not being parsed

Here is an code to reproduce this bug

$(document).ready(function() {
  $("#file").on("change", function(e) {
    const file = e.target.files[0]
    
    Papa.parse(file, {
      header: true,
      skipEmptyLines: true,
      trimHeaders: true,
      complete: ({ data: json, errors: e }) => {
        if (e[0]) {
          console.log('failed to parse', file.name);
        } else {
          console.log('parsed', file.name);
        }
      },
      error: (e) => {
        console.log('failed to parse', file.name);
      }
    })
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>

<input type="file" id="file">

And here is the GIF of the issue

enter image description here


Solution

  • I solved this by setting e.target.files[0] = undefined on any error