I am trying to read a text file that is in the same directory as my html file using javascript so that I might include the contents of the text file in my html file.
Here is the code I have to test the fopen and fread functions
<html>
<head>
</head>
<body>
<script>
fh = fopen('my.txt', 0); // Open the file for reading.
if(fh!=-1) // Check 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);
}
</script>
</body>
</html>
I've tried replacing the 'write' with document.write and still nothing.
Here are some websites that had this code as an example:
.htm
Any help at all would be much appreciated.
Thank you!
I am trying to read a text file that is in the same directory as my html file using javascript so that I might include the contents of the text file in my html file.
Here is the code I have to test the fopen and fread functions
<html>
<head>
</head>
<body>
<script>
fh = fopen('my.txt', 0); // Open the file for reading.
if(fh!=-1) // Check 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);
}
</script>
</body>
</html>
I've tried replacing the 'write' with document.write and still nothing.
Here are some websites that had this code as an example:
http://answers.yahoo./question/index?qid=20130519190823AA2lQ1W
http://www.c-point./JavaScript/articles/file_access_with_JavaScript.htm
Any help at all would be much appreciated.
Thank you!
Share Improve this question edited Aug 11, 2021 at 16:18 Johann Lau 1932 silver badges16 bronze badges asked Oct 11, 2013 at 2:14 Alex AliAlex Ali 31 gold badge1 silver badge2 bronze badges 1- The code snippet you've picked up is not Javascript. This example won't work, and the Yahoo site is just wrong. You can do this in PHP, or you can do it in Javascript if you use an AJAX call. – user1864610 Commented Oct 11, 2013 at 2:20
3 Answers
Reset to default 1Javascript has no filesystem access. As it is mentioned in the second link you posted, you will need to install special plugins in order to give JS file system access.
I don't think it is the right way to acplish whatever you are trying to do.
In order to access client's filesystem, the popular way I've seen is using Flash or Java applet or Microsoft Silverlight for that matter.
For accessing your server filesystem, you will need to run a web server which has proper permissions to access the filesystem. Then, you can make AJAX calls to the web server, which in turn will fetch the file for you.
As Apoorv said, JavaScript has no filesystem access. But I think it is important to consider why that is. Or rather, ask yourself, would you go to a website that could access files on your machine?
Functions like fopen is not defined in web browsers. You cannot access file system from javascript. Either have to do something like this: Question or load your files with ajax
Either way you cannot load file's from viewer's puter, only from your server. Again either way trying to load from a different server will also result in cross origin related limitations.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745327633a4622732.html
评论列表(0条)