3

I have the following directory structure for my application:

  • css
  • files
  • js
  • images
  • index.html

my csv file is located in the folder "files". I want my js to pick the csv file automatically from the directory and pass that file to Papa parse.

I need to implement this because this is the requirement. I am not allowed to pick the file using input tag of html.

Please let me know if it is possible, then how can implement this. If not possible then please let me know the another way to do this.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
SPGuar
  • 353
  • 6
  • 17

1 Answers1

0

If this is on your local machine, you must set up a simple server which will allow your JS file to read it. This can be done as described below:

  1. Navigate to the folder that contains your CSV files
  2. Within that folder, run the command $ python3 -m http.server
  3. This will allow the files to be available over the link http://lvh.me:8000/your_csv_file.csv
  4. Next, in your JS file using Papa Parse, paste the following:

    Papa.parse("http://lvh.me:8000/your_csv_file.csv", {
        //parameters to set
        download: true,
        header: true,
    complete: function(results) { console.log(results); } });

  5. On Chrome, you must enable cross-origin resource sharing. You could use the app Allow-Control-Allow-Origin
  6. Using Chrome, navigate to your webpage and look at the console log. The content of your CSV file will be there!
Ali Nobari
  • 1,921
  • 1
  • 9
  • 8