got some great feedback last time.
I'm essentially setting up a Wiki-type system on my local computer. I'm linking between HTML files, with some already existing and some not. I'm using XMLHttpRequest to check to see if the files exist.
The problem comes when I need to check files that are in different directories. I've come to find out this is a security thing in place. However, I'd like to disable it if possible, just for my local files. It seems rather odd that I can't have simple Javascript that returns the text on any web page at all, but even more so on my own computer.
I don't have any sort of server software running, just a bundle of HTML files with Javascript in them.
Thanks in advance!
javascript can not talk to the local filesystem. The reason for this is to keep people from writing destructive scripts that break your computer.
If this isn't a learning exercise, perhaps you might be interested in TiddlyWiki?
As you mention your self, "this is a security thing in place". In general, JavaScript is not allowed to access local files as it violates the sandbox.
However, have a look at these SO posts that discuss a few possible solutions:
Can JavaScript access a filesystem?
Local file access with javascript
You can also have a look at this HTML5 feature:
Reading local files in JavaScript
Related
I'm continuing the project I talked about in another thread here, but at the moment I have stopped because I can't figure out how to dynamically add content to the page. It's supposed to be a webapp that runs locally (it won't be hosted on a domain).
I thought I'd be able to do it with JS, however I found out it can't read files for security reasons. What I was planning to do was get a list of all the files and folders inside a given directory (using JS), then append a div for each folder (with more stuff inside) inside the content area of my page. Seeing how I couldn't do that with JS, I thought maybe I could do it with PHP using the same approach, but I didn't like the idea of having to run xampp just for this... if there's a way to have PHP running without xampp and/or a server, then it'd be great, but so far I haven't found anything about that... except downloading PHP, but I have no clue how to "use" the console from the browser and then have it append the divs to the page, since the console and the browser are "separated".
To make it a little more clear: is there a way to run PHP without a server, and if not, what other languages can I look up to?
At this point I'd also like to know how websites such as clipchamp.com work. Do you guys have a clue about that?
In order to run PHP, you'll indeed need a server with the right permissions on the folder.
For small local projects, I've seen Node.js used, the learning curve isn't too steep if you already know JS. Check out the fs module in Node for access to the filesystem.
I thought I'd be able to do it with JS, however I found out it can't read files for security reasons.
It can, just not when the JS itself is executing in a web browser. JS running in, for example, Windows Scripting Host or NodeJS would have no problems.
if there's a way to have PHP running without xampp and/or a server
You can run PHP from the command line, but if you want the output to be rendered in a browser then you really need to be dealing with HTTP, which requires a server.
There is always the option of PHP's built-in webserver.
is there a way to run PHP without a server, and if not, what other languages can I look up to?
It doesn't matter what language you use. You'll find it hard to pick one that can't read the file system. They will all suffer from the same limitations when it comes to interacting with a web browser though. You need to be talking HTTP.
That is the whole concept of a browser. A browser can not give more permission to the system then the user of the browser does.
You have afaik two options: Use a server on the machine you want to manipulate (there are all kind of flavors, you can use nearly every language for building a server) or use/create a browser plugin that can do it (Silverlight can have access to the file system under some restrictions).
I have a single text input box where user enters a path to a directory. I want to fetch names of all the files from that directory using File API in JavaScript.
I was reading from this article: http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-dir and tried executing the code under Reading a Directory's contents but was unable to understand the code since we are mentioning the directory name anywhere in the code.
So, how can i accomplish my task?
As far as the SPECS are concerned,
It is not possible to read normal file system directories from the browser apart from the upload button / flash etc.
The browser can, however create a sandbox (in its user files) which appears as a directory structure you can manipulate. This is useful for apps that need to play with actual files and store them locally rather than on server.
Real Life story
The filesystem API is not supported, rather dead specs which may find its way to implementation if need arise.
Nearest currently working functionality is local storage
I have a text file i want to read in my html page,both are in the same directory and i am not running a server. I intend that my users use the script offline(basically text manipulation based on expressions and preserving new line characters) .
I tried ajax call but mostly cross domain origin problem occured and i know most of the users will have this security tighened up in many browsers , so its not of use to circumvent this in only in my browser.
I want to support many browsers including old browsers as in IE7,8 etc which do not support HTML5 filereader.for the same reason reading using filesystemobject or activex is not good.
Reading the file after user select it as input , is this possible?Otherwise i would have no option then using other technologies like php,java etc which may expect my user to setup these.
Please excuse me if i am repeating this but i am a beginner web developer. I know that reading local files via javascript is not possible but is there any other way?
If you can't support FileReader, then the answer is pretty much no (at least, if you want to support a large range of browsers rather than rely on convenient feature x of browser y). Unless : you indeed increase the requirements for running the application and get some sort of local server going (for instance node.js, Apache, TomCat, etc. but like you said this will greatly increase the requirements and become cumbersome for users).
You could also rethink what it is you're trying to achieve. What are the contents of the file you want to read ? Can't these contents be part of the HTML file you're serving to your users (i.e. a large JSON Object inside a script-tag ?)
On possibility of using node.js:
Node.js is quite easy to install and assuming you are requiring from your users to install it, you can use it as a local server, which is a nodejs script of about two lines in size :). Running it locally would also omit the need to upload anything anywhere as you can directly read from the file system using the fs-object (see sitepoint.com/accessing-the-file-system-in-node-js).
STILL: from both a design and ease-of-use-point of view you might want to resort to using either another technology, or include the text file content inside the HMTL file
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.
How do I read a local text file in JavaScript, w/o jquery etc.? I realize the question has been asked before, with a gazillion answers (but both html and javascript also have changed a lot over the years).
So here is my scenario:
do not want to use any webserver for this purpose as I wish to send a tarball+make install to someone for a demo and want zero installation
a blocking read is fine
files are simple text files such as "downloaded/xyz.json". They are indeed stringify'd json. Do not want to use a files dialog box to select file name - hardcoding is the desirable for this purpose
Security is not an issue in this context, although I am not prepared to run the browser with --disable-web-security or do anything that's even remotely cross-domain
env: linux (openSuse 11.4), using google-chrome
do not want to use jquery for this
Will greatly appreciate a simple code snippet, if it is feasible.