3

I can't find a single bit of information about how to do this, so I'm resorting to asking a question here.

How do I actually load a local file and parse it using PapaParse?

I currently have:

$(function() {
    Papa.parse(new File("text.csv"), {
        complete: function(results) 
            console.log("Finished:", results.data);
        }
    });
});

...but I have decided the constructor must be intended for creating new files. How am I supposed to do this? PapaParse's website shows this:

Papa.parse(file, {
    complete: function(results) {
        console.log("Finished:", results.data);
    }
});

...buuut that doesn't explain where file came from.

Clonkex
  • 3,373
  • 7
  • 38
  • 55
  • the docs http://papaparse.com/docs#local-files say its looking for a File object from the DOM, ie. a File API object https://developer.mozilla.org/en-US/docs/Web/API/File – haxxxton Aug 02 '16 at 05:56
  • @haxxxton Thanks, I saw that, but I have not been able to figure out how you make/get a File object. – Clonkex Aug 02 '16 at 06:20

1 Answers1

4

You need to read the file from the system before trying to parse it.

If you are trying this in a web application, you could add a file uploader and call Papa.parse() after an event: i.e. either when file changes, or at the click of a button.

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

Then you need to add an onchange or onclick event function accordingly.

Inside the event function, you can access the file as:

event.target.files[0]

If you are using this on a server, you can use any filesystem methods to read your file.

Like in Node.js we have fs.readFile() function to read a file from the system at a given path.

ryder
  • 897
  • 6
  • 15
  • Ah, I see. I think my confusion was that I forgot JS couldn't natively read files straight from the filesystem. I was trying to make a simple tool to aid my work. I've found [Electron](http://electron.atom.io/), and it seems pretty easy to use, so I'll see what I can achieve with that :) – Clonkex Aug 02 '16 at 06:21