2

Hi all i just want to know if we can read a file using javascript like

what we do

fp=("r","path")

like that is it possible?

Harish
  • 2,311
  • 4
  • 23
  • 28

6 Answers6

5

No, that's not possible in a browser. Javascript runs in a sandboxed environment and doesn't have access to the file system. You might need to special plugins to be installed on the client browser in order to access his file system.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Some browsers support the File API, which allows the user to explicitly grant permission to the browser to read a local file; see my answer for details. – T.J. Crowder Feb 17 '11 at 12:50
3

Yes, this is possible, even in some browsers.

Reading a local file, from a browser

If the browser supports the new File API, you can read any file the user gives you permission to read via an input[type=file] element. Specification | Example here on StackOverflow

Read a server file, from a browser

This can be done on all major browsers using "ajax", more specifically the XMLHttpRequest object. It's made a lot easier by libraries like jQuery, Prototype, YUI, Closure, or any of several others.

On a server, workstation, etc. (not in a browser)

You'll need an environment that provides file reading, such as NodeJS.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Reading from client: how to read a text file using Javascript

Reading from server: jquery - Read a text file?

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
2

Firstly I think its not a good idea to read a file locally with JavaScript. I recommend first upload it to the server and then perform the reading.

Having said that it is possible, but you restricted by what you can do.

Im assuming its a local file on the user machine, otherwise AJAX would achieve this for a server read.

It might be possible through

  1. Windows Script Host Object Model(WScript.Shell) and when granted Prompt or Enable access to ActiveX the browser has elevated privileges (Enable through Tools > Internet Options > Security > Custom Level ... > Set Active X settings to prompt). If this is still to difficult, user could download something thats installed and then does the reading through Shell Scripting! Disclaimer: Note I do not recommend this approach. Its not active for a reason and its so DIRTY (I feel dirty)!

  2. Cookies might also be worth considering. If you can store the information in a cookie the JavaScript would be able to read, write and update it.

    Found this code from http://www.quirksmode.org/js/cookies.html scroll right to the bottom for the example.

    function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    

    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
    
    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
    
Jonathan
  • 2,318
  • 7
  • 25
  • 44
0

Reading client files in javascript is possible with the new File API available in modern browsers. Check this site and its code: http://www.readfileonline.com/

dev4life
  • 10,785
  • 6
  • 60
  • 73
-2

You can but then you have to use AJAX, which is Javascript for server side jobs.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Shaoz
  • 10,573
  • 26
  • 72
  • 100
  • AJAX is not "javscript for server side jobs". One good and correct definition is *group of interrelated web development techniques used on the client-side to create interactive web applications* (Source: Wikipedia) BTW the downvote is not mine. – Shadow The GPT Wizard Feb 17 '11 at 13:33