Reading file from javascript [modified]
-
Does anybody know how to read a file using javascript I have tried but it seems not to work
fh = fopen("filepath", 0); // Open the file for reading if(fh!=-1) // If the file has been successfully opened { length = flength(fh); // Get the length of the file str = fread(fh, length); // Read in the entire file fclose(fh); // Close the file // Display the contents of the file write(str); } -- modified at 10:48 Monday 10th July, 2006
-
Does anybody know how to read a file using javascript I have tried but it seems not to work
fh = fopen("filepath", 0); // Open the file for reading if(fh!=-1) // If the file has been successfully opened { length = flength(fh); // Get the length of the file str = fread(fh, length); // Read in the entire file fclose(fh); // Close the file // Display the contents of the file write(str); } -- modified at 10:48 Monday 10th July, 2006
If you are running this through a web page you probably won't have permissions to open a client's file system.
-
If you are running this through a web page you probably won't have permissions to open a client's file system.
-
I have an input element of type file, so i can get the file path on the client side, but i dont know how to read the file on the client's side. Im using Jboss as my container, and im using JSPs. Any help.
-
Does anybody know how to read a file using javascript I have tried but it seems not to work
fh = fopen("filepath", 0); // Open the file for reading if(fh!=-1) // If the file has been successfully opened { length = flength(fh); // Get the length of the file str = fread(fh, length); // Read in the entire file fclose(fh); // Close the file // Display the contents of the file write(str); } -- modified at 10:48 Monday 10th July, 2006
As far as I am aware, javascript is forbidden access to your file system except by using an Active X control. In which case, taken from an older version of JScript : OpenTextFile Method ============================= var fs, a, ForAppending; ForAppending = 8; fs = new ActiveXObject("Scripting.FileSystemObject"); a = fs.OpenTextFile("c:\\testfile.txt", ForAppending, false); ... a.Close() FileExists method ========================= function ReportFileStatus(filespec) { var fso, s = filespec; fso = new ActiveXObject("Scripting.FileSystemObject"); if (fso.FileExists(filespec)) s += " exists."; else s += " doesn't exist."; return(s); } Write method: ============================= function WriteDemo() { var fso, f, r var ForReading = 1, ForWriting = 2; fso = new ActiveXObject("Scripting.FileSystemObject") f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true) f.Write("Hello world!"); f.Close(); f = fso.OpenTextFile("c:\\testfile.txt", ForReading); r = f.ReadLine(); return(r); }
-
That doesn't work. If you run the Javascript in the browser, you can't access any file system. If you run the Javascript in the server, you can't access the file system of the client, only the server. --- b { font-weight: normal; }
Yeah, you'll have to upload to the server and do something with the file there. You can't use JavaScript to read files on a client machine. I think that Flash might be able to do that.. anyone seen this lately? Dirk Watkins
-
Yeah, you'll have to upload to the server and do something with the file there. You can't use JavaScript to read files on a client machine. I think that Flash might be able to do that.. anyone seen this lately? Dirk Watkins