在多个站点上找到的关于 inf fopen 的 Javascript 示例对我不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19309187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:06:34  来源:igfitidea点击:

Javascript examples found on severial sites regardinf fopen is not working for me

javascriptfilefopenfread

提问by Alex Ali

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.

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

Here is the code I have to test the fopen and fread functions

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

<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.

我试过用 document.write 替换“write”,但仍然没有。

Here are some websites that had this code as an example:

以下是一些以此代码为例的网站:

http://answers.yahoo.com/question/index?qid=20130519190823AA2lQ1W

http://answers.yahoo.com/question/index?qid=20130519190823AA2lQ1W

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

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

Any help at all would be much appreciated.

任何帮助都将不胜感激。

THANK YOU!!!

谢谢!!!

回答by Apoorv Parijat

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.

Javascript 没有文件系统访问权限。正如您发布的第二个链接中所述,您需要安装特殊插件才能访问 JS 文件系统。

I don't think it is the right way to accomplish 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.

为了访问客户端的文件系统,我见过的流行方式是使用 Flash 或 Java 小程序或 Microsoft Silverlight。

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.

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

回答by gwg

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?

正如 Apoorv 所说,JavaScript 没有文件系统访问权限。但我认为重要的是要考虑为什么会这样。或者更确切地说,问问自己,您会访问可以访问您机器上文件的网站吗?

回答by Quad

Functions like fopen is not defined in web browsers. You cannot access file system from javascript. Either have to do something like this: Questionor load your files with ajax

网络浏览器中没有定义 fopen 之类的功能。您无法从 javascript 访问文件系统。要么必须做这样的事情:问题或用ajax加载你的文件

Either way you cannot load file's from viewer's computer, only from your server. Again either way trying to load from a different server will also result in cross origin related limitations.

无论哪种方式,您都无法从查看器的计算机加载文件,只能从您的服务器加载。同样,尝试从不同服务器加载的任何一种方式也将导致与跨源相关的限制。