HTML 5 File API - javascript

I hear that Firefox 3.6 adds support for the HTML local file API (Announcement here).
Does this mean that I can access local files from javascript?
Can anyone point me to examples for reading / writing local files?
I would love to be able to read / write simple text files.

The only files you can access are files dropped from the desktop or files that has been selected from an <input/> tag. See the demo I've written here: http://hacks.mozilla.org/2009/12/uploading-files-with-xmlhttprequest/

It doesn't allow accessing arbitrary local files. It's more of a file upload improvement. For instance, you can have some Javascript accept a drag-and-drop file initiated by the user of the browser.
You can find the W3C spec on it here.

Take a look at the demo at hacks.mozilla.org.

You can find a (mozilla-specific explanation of the API on the Mozilla Development Center.

It's possible to write to local file system, but the user must complete a 'save as...' action. For now, no direct access to client file system. Take a look here

Related

Client-only Dropbox access

I am trying to write an utility that logs into the user's Dropbox account and displays the files there (with some additional formatting, etc) in a browser. I would like to do this with only client-side technologies without any server-side code. Is this possible?
I found this: http://code.google.com/p/dropbox-js/ - haven't tried it yet. It doesn't have much documentation on how to get started. Any other alternatives?
The Chooser API will not allow you to apply custom formatting to files.
If you want to display the user's entire Dropbox, create an application with Full Dropbox access, and use dropbox.js to read the user's Dropbox.
This walks you through setting up your application: https://github.com/dropbox/dropbox-js/blob/master/doc/getting_started.md
This particular snippet (List a Directory's Contents) is most relevant to you: https://github.com/dropbox/dropbox-js/blob/master/doc/getting_started.md#list-a-directorys-contents
readdir documentation: http://coffeedoc.info/github/dropbox/dropbox-js/master/classes/Dropbox/Client.html#readdir-instance
The checkbox.js sample application uses readdir here: https://github.com/dropbox/dropbox-js/blob/master/samples/checkbox.js/public/checkbox.coffee#L135
If you run into roadblocks, open an issue on the dropbox.js GitHub page.
Check this out:
https://www.dropbox.com/developers/chooser
They have a specialised file for this. Try the demo and start using.

work on the files in a directory using html javascript

Trying to see if this is possible -
1.) User logs into our site
2.) Points to a directory
3.) The javascript code reads contents of the directory, shows thumbnails for any jpeg/gif in those directories. This all without uploading all the photos to the server. Kind of a semi desktop app.
Point 3 is something I have never done, is this possible for an online application to do ?
You can't do this...yet (unless you're using a prerelease of Chrome 9). There are some APIs coming down the pike that will make this possible in browsers that support them; there's a description of using them in this article.
But right now, no. To do this, you'll need to use a technology that allows local file access, such as a signed Java applet (normal unsigned ones obviously can't do this) or, on a severely limited number of platforms and browsers, an ActiveX control.
Update: Sorry, the new JavaScript APIs I mentioned above don't give you (user-granted) access to any old directory on their system. They do give you access to the file system, but it's a sandboxed file system. So you'd have to have the users move the files into the sandbox (which you could do via the File API and with drag-and-drop, keeping it an entirely client-side thing, no uploading required). But that isn't quite what you described.
No; this is not possible.
Javascript cannot directly interact with the user's local filesystem
No, it is not possible to access any of the clients files using JavaScript as that would be a security risk.

How can we write to local disk (ie on files on the client) using Javascript

I need to save some data from a Chrome Extension either to a local disk file or Google Spreadsheets. The problem I am facing is being able to find a right solution for writing to files using javascript since it is considered to be a security hole and generally not adviced. I need help in figuring on a method to write to local files using Javascript (if it is possible in the first place or else please suggest and alternative to this (like any method on how to save data from an extension to Google Spreadsheet).
Thanks in advance
You can't for security reasons. If you could, this would be the ultimate back-door.
You can do this with a signed java applet. [You can also delete/modify/anything user's files].

Writing to a file

I am extracting content from a web page using JS/JQuery and want to write the data to a comma separated file. I know javascript doesn't really support file io and I'm trying to avoid setting up a local apache server just to process the content to a file. Is there an easier way to do this?
You can have your JS create the file text in a string. Then open a new window and write the string into the new window.
You can write a Mozilla Extension.
PROs:
You can use JS/JQuery on the extension
You can write files on the extension
CONs:
You can only use the application in Firefox (or any other Mozilla based browser).
There is no simple way of doing this. W3C has recently published the first draft of a File API that will make it possible, but it'll be a while until this is through and widely implemented.
Still requires a webserver, but you could build a small one method web service that your js calls, then the service can write out the data.

Edit a file using javascript

I need to edit an xml file using javascript. Now I'm involved in a project of online testing.
The question.xml file is already in the project folder.
In that i want to add or edit the questions(only using javascript). I'm able to bring the particular content through ajax, but I am not able to edit the file.
Javascript can't write to a file. The best you'll be able to do is get Javascript to read and edit the XML then post that data to a server-side script to write to file.
Until now, Google Chrome is the only web browser that has a functioning implementation of the FileSystem API, therefore, it may allow you to save files locally using only Javascript.
Obviously, for security reasons, when writing files to the local file system, the user must explicitly allow it.
A working tutorial: http://www.html5rocks.com/en/tutorials/file/filesystem/
Nickf is correct. The reason Javascript can't write to a file is because it is a Client-Side language. Javascript will never have permission to write a file because it has to operate inside the browser sandbox.
You will need to use a server-side script (.NET, PHP, ColdFusion, etc) to write the file.
If you are willing to use Google Gears, you get a sandbox on the client machine on which you can write files.
Javascript has no built-in file I/O (a.k.a. you can't do it with JS alone)
Instead use some kind of server side language such as PHP or ASP.NET in conjunction with Javascript's AJAX functionality.
Look over Adobe's Flex development system. There are ways you can use it to build an app that runs in the browser (or not) and can access the filesystem (Windows/Mac/Linux). It's programmed in ActionScript, a dialect of javascript; and can interoperate with javascript in the browser.

Categories