javascript 如何使用javascript读取文件的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5469111/
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
How to read contents of a file using javascript?
提问by Sangam254
I have an input type="file"
button. After I choose a file, I have to read the contents of the file using javascript. Is it possible to read/get contents of a chosen file using javascript or ajax?
我有一个input type="file"
按钮。选择文件后,我必须使用 javascript 读取文件的内容。是否可以使用 javascript 或 ajax 读取/获取所选文件的内容?
回答by Zeta Two
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. It is not available in all browsers yet though.
在某种程度上,你们都错了。有可能的。使用新的文件 API,您可以在将文件提交到服务器之前读取文件。不过,它并非在所有浏览器中都可用。
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.
编辑:即使问题指出“上传的文件”,我也将其解释为“要上传的文件”。否则根本没有意义。
回答by Pranay Rana
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");
}
});
回答by yzxben
Reading files client side is hard:
读取文件客户端很困难:
How to read and write into file using JavaScript
Local file access with javascript
Unless you are trying to do it with local javascript:
除非您尝试使用本地 javascript 来执行此操作:
Access Local Files with Local 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 对象:
回答by DoXicK
you cant do it using javascript directly. You can post the file to the server an then use ajax to retrieve the content.
你不能直接使用 javascript 来做到这一点。您可以将文件发布到服务器,然后使用 ajax 检索内容。
回答by Lindsay Morsillo
For IE use the FileSystemObject (which is found on all Windows systems).
对于 IE,请使用 FileSystemObject(可在所有 Windows 系统上找到)。
For 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 TiddlyWikiapp to see how it does it across all major browsers.
要查看这些方法和其他正在使用的方法,请查看TiddlyWiki应用程序以了解它如何在所有主要浏览器中执行此操作。
回答by Ben
Javascript is designed not to have access to the computer it is running on. This is so that rogue javascript can't read the user's harddrive. You could look into using iframes though.
Javascript 被设计为无法访问它正在运行的计算机。这是为了使流氓 javascript 无法读取用户的硬盘驱动器。不过,您可以考虑使用 iframe。
回答by ashishjmeshram
It is not possible to do it in java script. See Local file access with javascript
在java脚本中是不可能的。请参阅使用 javascript 访问本地文件
I agree with DoXicK above. You can post the file first on server and then you can use Ajax to read it.
我同意上面的 DoXicK。您可以先将文件发布到服务器上,然后您可以使用 Ajax 读取它。
回答by Robin Maben
That is not entirely impossible
这并非完全不可能
A browser's usually runs Javascript(JavaScript Engine) in a sandboxed environment.
浏览器通常在沙盒环境中运行 Javascript(JavaScript 引擎)。
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 XMLHttpRequestobject.(in other words - Ajax)
或者将文件上传到您的服务器并使用XMLHttpRequest对象。(换句话说 - Ajax)