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.
1 Answer 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:
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/
Comments
Explore related questions
See similar questions with these tags.
file://protocol is restricted for security reasons.fetch. Second, no need for papaparse, d3 will parse that CSV.