xmlhttprequest for local files

Historically, you can’t query for local files from JavaScript (or shouldn’t be allowed to, or something’s odd). This would be a serious breach of security.

There are only a few circumstances where you can do this, but in general they involve specific security settings requiring to be set for your browser, to either lift the limitation or to notify the current page’s execution process that that is is granted this exceptional right. This is for instance doable in Firefox by editing the properties. It’s also commonly OK when developing browser extensions (for instance for Chrome or FF) if they request the file access permissions.

Another way to go around this limitation is to host a local web-server, and to declare virtual hosts on it to be able to do this sort of AJAX request to fetch local files. It’s quite common for web-developers to resort to this trick (more like a standard, really) to have the benefits of local development but at the same time replicate a production system. You could for instance use a lightweight web-server like Jetty.

(Another mean annoyance, that you seem to have encountered, is that some browsers – at least some relatively older FF versions, like 3.6.x – will sometimes return a positive error code like 200 when they requests are blocked according to their internal security policies. Can be pretty confusing for a while…).

Finally, the newer HTML5 APIs do provide some new constructs to access local files. Considering reading:

Other SO questions also provide additional pointers:

Leave a Comment