Is it possible to pull data from a CSV file locally automatically? - javascript

I am attempting to put data from a CSV file and put it into a String variable in an HTML file. The HTML and CSV file are under the same local directory on my computer. In Java and C++, I know it is possible to read and write to a text file. So, when using JavaScript, PHP, jQuery, or anything else, is it possible to set the string in my HTML file equal to the data from a local CSV file? I need a way to read the CSV text into a Javascript variable string.
I have a program currently that parses CSV formatted text, but I need the text to be pulled from a CSV file. I have read numerous posts saying that this is not possible because of security reasons - is this the case? All of the answers that I have seen require the user to select the file the want to pull from. I just want it to automatically be pulled from a specific CSV file always.

I'm sorry to have to disappoint you, but all the other comments where right. An app running in a browser is strictly not allowed to access the local file system without any user interaction. For the formerly mentioned security reasons. There is no way around, though.

Everyone was right - it is impossible to do it the way I was trying. However, I decided to host a local server on my PC using Python, and it worked like a charm! Credit to: https://youtu.be/1OK4TJfCzdY .

Related

How can I open and parse a local csv file into an array?

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.
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/

How can I get files and file names using JavaScript?

I am making a website that contains mainly static articles, and I wanted to just access .txt files from a directory and have the pages dynamically created that way when the site is loaded using JavaScript. However, I am struggling to find a way to get access to the files (The files would be in a folder wherever the site is being hosted along with the actual web pages and then I would be extracting the text from these files and creating the pages that way). Is this at all possible without having to use server side code or are there any recommendations for creating the pages another way? I would really like to avoid going through all of the trouble of continually adding html pages for every single article as that would be pretty monotonous and I currently don't have any back-end code and would like to keep it that way if possible. Any recommendations in general would be appreciated.
This is not possible without a backend server. Code running in browser is not allowed to access the files on the system due to security reasons.
Either write a simple backend service to fetch those text file or access those text file data by converting the text files as JavaScript files and declaring text data as variables. Then load these files before your main JavaScript file. Now you can access the data as variables in your main js script.
It is not possible in the traditional open file, read file and close file paradigm.
You could create a web service on your server that would return data from a file using ajax, such as the contents of the text file. But without any backend code, it will not be possible since there would be a lot of security implications if the client-side code was able to do so without any server-side code.

Detect local changes of JavaScript code

I have a website (game in JS). I would like to know if it's possible to detect if some client has edited and saved JavaScript file locally. My idea was to send the entire (or probably just part, it doesn't matter right now) .js file as POST parameter, so that PHP could compare it with .js file on server.
I've searched for local modifications, mutationobserver etc. but I haven't found the way.
Also, can JS file read itself?

Read local JSON file (no web-server) with JavaScript

It seems that this has been asked a number of times, but I would like to make sure there are still no other alternatives.
So what I need to do is:
Generate a .JSON file from .net (easy)
Read the local JSON file with JavaScript and produce an html page with useful graphs etc (using Raphaeljs).
Deliver the outcome (HTML + associated JavaScript) to the client.
I wouldn't mind installing a local Apache to read the JSON file easily but since I cannot ask the client to do this, I am looking for a way to read a local JSON file (on the filesystem, no web server) with JavaScript, so that I eventually provide the client with the HTML, .js and .json and he can see the associated graphs.
I 've been reading that FileReader class is an option but I want to have the flexibility to support these on a number of IE browsers (let say IE7 to IE10).
Any help on how to work round this would be much appreciated.
Web browsers are not able to read files from the file system due to security reasons. If I've understood correctly, you have a .NET application running on the local machine?
If this is the case, you can host a WCF service to serve the JSON from your application. WCF self-hosting is described on this MSDN page. You should be able to make AJAX requests from the browser to a server hosted within you application on localhost.
If you want to support older IE, you need to go into ActiveX Security setting hell. Look into var fso = new ActiveXObject("Scripting.FileSystemObject");
But if you are delivering the content and HTML files to the client, why don't you just add the JSON markup directly into the html document? Seems like the EASY solution.
You can't read file from local disk in browser (security restriction). But once you are going to deliver HTML + js and JSON (in zip?) you can read json like from regular web server. Just use relative links and if JSON will be in same folder where HTML and JS is located. Or you may put your JSON as a part of HTML you deliver to customer.
But if you are going to show a page (HTML and JS) on your site in web, you should better ask client to upload their JSON file, and than download it from server (in AJAX style).

how to use javascript to open a folder and list html file names in that?

I want to list the names of HTML files in a particular folder using JavaScript (in the browser)...
Can anybody help me in this?
Thank You
If you're using Javascript that's running in the browser, there's no way to "open a folder". You have to obtain data about the folder contents through some data structure or by doing something like parsing a server generated folder index in HTML.
If you're using Javascript that's running on a non-browser engine (such as jscript, rhino, etc.) then you have to be more specific in the question as the answer will obviously depend on whether the engine where your script is running provides objects to access the filesystem or not.
This is what you are asking for:
http://www.irt.org/articles/js014/index.htm
Also read on FSO
But yes, if you can't use FSO if ActiveX is disabled on client system.
Javascript can't interact with the OS while running inside a browser, due to security concerns.
It can get a list from the server and display that to the client on the browser, but, to get a directory structure from the client computer to the browser would require some other application reading the directory structure and sending it to the server, and the server sending it to the browser.
Now, if you are using Actionscript it is possible to read the hard drive, and I expect that Silverlight would allow you to do this also, but I don't know.

Categories

Resources