简体   繁体   English

如何使用javascript读取文件的内容?

[英]How to read contents of a file using javascript?

I have an input type="file" button. 我有一个input type="file"按钮。 After I choose a file, I have to read the contents of the file using javascript. 选择文件后,我必须使用javascript读取文件的内容。 Is it possible to read/get contents of a chosen file using javascript or ajax? 是否可以使用javascript或ajax读取/获取所选文件的内容?

You are all wrong in a way. 你在某种程度上都是错的。 It is possible. 有可能的。 With the new File API you can read files before submitting them to the server. 使用新的File API,您可以在将文件提交到服务器之前读取文件。 It is not available in all browsers yet though. 但它并不适用于所有浏览器。

Check this example. 检查此示例。 Try to open a text file for example. 例如,尝试打开文本文件。

http://development.zeta-two.com/stable/file-api/file.html http://development.zeta-two.com/stable/file-api/file.html

Edit: Even though the question states "uploaded file" I interpret it as, "a file to be uploaded". 编辑:即使问题陈述“上传文件”我将其解释为“要上传的文件”。 Otherwise it doesn't make sense at all. 否则它完全没有意义。

With AJAX its possible to read uploaded file but with pure javascript its not possible because javascript works on client side not on sever side. 使用AJAX可以读取上传的文件但是使用纯javascript它是不可能的,因为javascript在客户端而不是在服务器端工作。

if you are going to use jquery than Ajax call may be like this 如果你要使用jquery而不是Ajax调用可能是这样的

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});

Reading files client side is hard: 阅读文件客户端很难:

How to read and write into file using JavaScript 如何使用JavaScript读写文件

Read a local file 读取本地文件

Local file access with javascript 使用javascript进行本地文件访问

Unless you are trying to do it with local javascript: 除非您尝试使用本地javascript执行此操作:

Access Local Files with Local Javascript 使用本地Javascript访问本地文件

Or server side javascript: 或服务器端javascript:

http://en.wikipedia.org/wiki/Server-side_JavaScript http://en.wikipedia.org/wiki/Server-side_JavaScript

Alternatively you can force your user to install an ActiveX object: 或者,您可以强制用户安装ActiveX对象:

http://4umi.com/web/javascript/fileread.php http://4umi.com/web/javascript/fileread.php

you cant do it using javascript directly. 你不能直接使用javascript。 You can post the file to the server an then use ajax to retrieve the content. 您可以将文件发布到服务器,然后使用ajax检索内容。

Javascript is designed not to have access to the computer it is running on. Javascript旨在无法访问其运行的计算机。 This is so that rogue javascript can't read the user's harddrive. 这是流氓javascript无法读取用户的硬盘。 You could look into using iframes though. 你可以考虑使用iframe。

It is not possible to do it in java script. 在java脚本中无法执行此操作。 See Local file access with javascript 请参阅使用javascript进行本地文件访问

I agree with DoXicK above. 我同意上面的DoXicK。 You can post the file first on server and then you can use Ajax to read it. 您可以先在服务器上发布文件,然后使用Ajax读取它。

That is not entirely impossible 这并非完全不可能

A browser's usually runs Javascript(JavaScript Engine) in a sandboxed environment. 浏览器通常在沙盒环境中运行Javascript(JavaScript Engine)。

So you can use Windows Scripting Host or Internet Explorer in a trusted environment and use the FileSystemObject 因此,您可以在受信任的环境中使用Windows Scripting Host或Internet Explorer,并使用FileSystemObject

or use 或使用

Or upload a file to your server and use the XMLHttpRequest object.(in other words - Ajax) 或者将文件上传到服务器并使用XMLHttpRequest对象。(换句话说 - Ajax)

For IE use the FileSystemObject (which is found on all Windows systems). 对于IE,使用FileSystemObject(可在所有Windows系统上找到)。

For Firefox: 对于Firefox:

var file = Components.classes["@mozilla.org/file/local;1"].
       createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("/home");

See https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO 请参阅https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO

To see these methods and others in use, look at TiddlyWiki app to see how it does it across all major browsers. 要查看这些方法和其他正在使用的方法,请查看TiddlyWiki应用程序,了解它如何在所有主流浏览器中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM