0

I'm trying to run a D3 visualization that reads data from a local .csv file (same directory as the javascript and HTML files), and I am looking for a way to parse this .csv file into an array of dictionaries than can then be fed to D3. However, all the solutions that I'm seeing in the Internet require either an or downloading the csv online/creating some kind of webserver or request, but I would like to avoid those. Is there any way to just open the file and parse it, sort of like in Python that you can just open('myfile.csv').

I would like to end up with a File object that can be passed to Papaparse. Thank you in advance, and sorry is this duplicated, I've been looking for this for a while so I thought I should try to ask it.

2
  • 2
    AFAIK no, it's not possible. You can't open files from local filesystems without input form element or AJAX request and a local server. The file:// protocol is restricted for security reasons. Commented Apr 21, 2022 at 14:39
  • 1
    Two things: you just need a server running on your machine to use d3's fetch. Second, no need for papaparse, d3 will parse that CSV. Commented Apr 21, 2022 at 23:28

1 Answer 1

1

When you are in the browser you run JS in a sandbox that has no access to the local filesystem. This is for security reasons. However, you can popup a file open dialog on the browser and import it in memory. Please check this SO thread and links:

Is it possible to upload a file in memory using javascript for processing before sending it to server?

For reference if thread is deleted:

https://developer.mozilla.org/en-US/docs/web/api/file

https://developer.mozilla.org/en-US/docs/web/api/file/using_files_from_web_applications

Also, https://techoverflow.net/2018/03/30/reading-an-uploaded-file-into-memory-using-pure-javascript/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.