在重要网站上发现的Javascript示例,如果fopen对我不起作用 [英] Javascript examples found on severial sites regardinf fopen is not working for me

查看:81
本文介绍了在重要网站上发现的Javascript示例,如果fopen对我不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript读取与我的html文件位于同一目录中的文本文件,以便我可以在我的html文件中包含文本文件的内容。

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.

以下是我必须测试fopen和fread函数的代码

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>

我尝试用document.write替换'write',但仍然没有。

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.com/question/index?qid=20130519190823AA2lQ1W

http://www.c-point.com/JavaScript/articles/ file_access_with_JavaScript.htm

任何帮助都会非常感激。

Any help at all would be much appreciated.

谢谢! !

推荐答案

Javascript没有文件系统访问权限。正如你在第二个链接中提到的那样,
你需要安装特殊的插件才能给JS文件系统访问权。

Javascript 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 accomplish whatever you are trying to do.

为了访问客户端的文件系统,我见过的流行方式是使用Flash或Java applet或者就此而言是Microsoft Silverlight。

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.

要访问服务器文件系统,您需要运行具有访问文件系统的适当权限的Web服务器。然后,您可以对Web服务器进行AJAX调用,然后Web服务器将为您提取文件。

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.

这篇关于在重要网站上发现的Javascript示例,如果fopen对我不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆