How to parse a CSV file into an array in Javascript - javascript

From what i understand, This usually has to be done server side. However i have read that it is possible to point to the csv file in a .js file. This would be more useful to me as i am not messing around with server side code and stuff for this project and will be all on my local machine. I saw an article with example code to do this somewere however i can't locate that article atm. Anyone know how to do this?

To access any file from the system you have to use directX in IE or read up on how to use NPAPI for mozilla support.If you are willing to use HTML5, check out its filereader API here.

Related

How do I read from external files in JS Bin?

I've been doing Code Year at Codecademy and I wanted to start practicing Javascript for myself, but I've been having a tough time figuring out some basic issues.
For my first project, I want to read in from a spreadsheet. I can't figure out how to access the data from its original source online, so I thought I would just save it as a text file. My question, then, is how to read from that.
So it looks like you can't read local files in Javascript. (Although apparently that's changing with HTML5? I don't have any familiarity with that.) So do I have to upload the text file someplace? Can I upload files to JS Bin? If not, does anyone have any recommendations for where I can upload the text file? And either way, once I do, what's the code to read from it?
Thanks in advance. I am sure this question is riddled with misstatements and improprieties, but I've spent a significant amount of time on this and I can't find anything that seems to answer my question. I honestly thought it would be something simple, like "var inputfile = c:\file.txt" but that seems not to be the case. I am totally lost. Thanks!
You can't. File system and storages in Javascript (or rather the client) is sandboxed.
That means you can only read what is written there in the first place. This has to do with security.
You will need to drop (or select) the local files into the browser and have some mechanism there to receive the drop/selection and store the file to one of the local storing mechanisms such as indexedDB or file API (the latter currently only supported in Chrome). For text files localStorage works fine too.
Resources:
http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side
http://www.w3schools.com/html/html5_webstorage.asp
http://www.w3.org/TR/webstorage/
http://www.w3.org/TR/FileAPI/
http://www.w3.org/TR/IndexedDB/
The other option is to upload it to server and download it from there when you need it.
When you get to this point in your development, its time to run your own webserver for testing as it makes things much easier. If you must insist on doing it your way, uploading the file to a file hosting site and reading it in is still possible. Codecademy is great for getting started, but when you get into dealing with persistent data sources (either files or databases) its time to get web hosting or set up your own test server.
Even then you don't REALLY need your own test server, just a folder on your computer. You can access the files with File://, and link in the file you want to read as a relative path. If the .txt file is in the same directory, you just link it in as "Example.txt" when you open the html file in that directory.

Can any body tell me how to open a FIle with .dat extension in cent os?

In my new Ruby on rails application I want to find the users country code.
So I am using MaxMind GeoIp. when I downloaded the gzip file after gunzip it gives me a GeoIP.dat file and I am stuck here. Can any one help.
If their is a program to open it or some procedure to use it.
Or if any one can suggest me the other way.
As #Kyle pointed out, you can download "human-readable" CSV files instead of binary DAT files. MaxMind's "GeoLite" downloads are here.
The CSV file format is described here.
But note (from the link above):
Due to the large size of geolocation databases, we generally recommend using our binary format with one of our APIs, since they are highly optimized for speed and disk space. On the other hand, if you have a requirement to import the data into a SQL database, the CSV format is recommended.
The APIs are listed here. There is no Javascript API listed, but there are a couple of options for Ruby.
So to answer your question directly: You would not "open" the dat file directly as you would a spreadsheet document. Instead you would write your own program that uses their API to read the dat file, and perform whatever tasks or queries you design it to do. Check out their API documentation for details of how you might get started with that.
.dat is just a file extension. The contents could be anything. Text. Binary data etc...
There is no way anyone could reliably tell you how to open the file.
I would attempt to view the contents of the file from the command line:
less file_name.dat
You can open the file and read line by line in ruby like this:
IO.readlines('file_name.dat').each do |line|
# do something with the line
end
Edit: I think I found the file you're refering to. Why not go here and download a csv version? The .dat version is not in plain text.

Include HTML file via JavaScript

This is a file stored locally, not on a server, so Server Side Includes do not work.
Problem:
I have an HTML file. There is lots of data in it, I want to split it into smaller parts, and then just include them all into my big html file, i.e. something like:
main.html
<include "partA.html">
<include "partB.html">
<include "partC.html">
And I want the result as if the contents of partA,B,C.html were read right into main.html
Now, this is not on a server -- it's stored locally, so I can't do SSI. My question is:
Is there some simple way to do this via JavaScript? It seems like with JavaScript, I shoudl be able to:
fetch the contents of blah.html [not sure how to do this ste[
call a document.write on it, to write it into the document
probably handle some stuff dealing with escaping strings
Question:
How do I do this?
Thanks!
It's not possible, as a security feature. This post here is a discussion on the topic - Includes without local server?. As the answers say, your best best is to install a small web server on the machine if you can. They're not too hard to get going.
I have used nginx before with good results. http://en.wikipedia.org/wiki/Nginx
HTML5Rocks has a tutorial on how to read local files using HTML5's File API:
http://www.html5rocks.com/en/tutorials/file/dndfiles/
Outside this, Javascript generally does not have the ability to access local filesystems.
Update - I assumed the main file was on a server, and you wanted that file to access local files. On re-reading, it appears all your files are local, in which case, some of the answers below will work.

How to read local (Client) file with GWT?

I would like to use GWT to read and parse local csv file.
EDIT: Local file here means client file.
I know I can use HTML5 with javascript to achieve that, but I would like to use GWT to do it.
One way of doing it is by using JavaScript Native Interface (JSNI), however I think it beats the purpose of using GWT.
Another way of doing this would be upload the file to server, and send it back as a json. But I feel it's not the best way of doing it.
Please advise. Thanks.
EDIT: I intend to design a desktop app, so I try to avoid using any web-server scripting.
Using jsni doesn't beat the purpose of gwt, sometimes you need to use it as a wrapper to functionality not yet available in gwt or an external javascript library. Instead of writting jsni access to the file api you can use one of several third party libraries available that implement html5 file access, like http://code.google.com/p/lib-gwt-file
A quick search returned this SO question: Read text file in google GWT?
Although GWT doesn't support java.io.InputStream, it looks like you could make a request to your CSV File and then parse it.
Remember that if you will host on AppEngine, you won't have a filesystem available so you'll have to have the files hosted elsewhere.

Needed help with javascript

So guys I am working on a program to like download stuff using javascript. I hav written the following code:
function download()
{
alert("Hello");
var url='http://somesite/somefile.rar';
window.open(url,'Download');
}
The code is pretty easy, but is there some other way to download the file using javascript? Also having downloaded the file is there some way to store it locally in the location of our choice, like d-drive? I had come across the javascript file api while searching the web, is it any useful in my scenario? Please help.
Edit: Fixed code formatting
No, this isn't possible. It is up to the client where to save files, not you.
Through Javascript, you can only set the file name that you want, but would not be able to access the file system i.e. All the folders, etc..

Categories

Resources