javascript 使用javascript处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10695934/
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
file handling using javascript
提问by r.bhardwaj
Possible Duplicate:
How to read and write into file using JavaScript
可能的重复:
如何使用 JavaScript 读取和写入文件
Is there any way to read from and write to a file (.txt or any other) using javascript.
For the clarificationI do not want to use JQuery libraries.
有什么方法可以使用javascript读取和写入文件(.txt 或任何其他文件)。
为了澄清起见,我不想使用JQuery 库。
回答by Eric J.
JavaScript executes in a sandbox to protect the computer from manipulation by evil people, so you cannot access the file system.
JavaScript 在沙箱中执行以保护计算机免受恶意人员的操纵,因此您无法访问文件系统。
You can however store things in DOM Local Storage. "Flash Cookies" (Flash Local Storage Objects) are also frequently used for a similar purpose.
但是,您可以将内容存储在DOM Local Storage 中。“Flash Cookies”(Flash 本地存储对象)也经常用于类似目的。
回答by zerkms
It is not possible. Browser javascript doesn't have access to the filesystem. Otherwise it would be a huge security breach.
这不可能。浏览器 javascript 无权访问文件系统。否则,这将是一个巨大的安全漏洞。
回答by jmort253
No, not with unprivileged JavaScript. Any JavaScript that loads in a Web browser via HTTP or HTTPS does not have the permissions to access the file system. This could have severe repercussions on a person's personal data security.
不,不是非特权 JavaScript。通过 HTTP 或 HTTPS 加载到 Web 浏览器中的任何 JavaScript 都没有访问文件系统的权限。这可能会对个人的个人数据安全产生严重影响。
However, JavaScript with Chrome permissions, such as JavaScript loaded via a chrome://
URL in a Firefox extension, Thunderbird extension, or a XULRunner application is what is known as priviledged JavaScript. This type of JavaScript can do things that ordinarily wouldn't be possible with JavaScript served on the Web. For instance, priviledged JavaScript can read and write to files, among other things.
但是,具有 Chrome 权限的 JavaScript,例如通过chrome://
Firefox 扩展、Thunderbird 扩展或 XULRunner 应用程序中的URL加载的 JavaScript ,就是所谓的特权 JavaScript。这种类型的 JavaScript 可以完成通常在 Web 上提供的 JavaScript 无法完成的事情。例如,特权 JavaScript 可以读取和写入文件等。
The primary factor that gives chrome-based JavaScript, or privileged JavaScript, the ability to do so much more is that user's must explicitly install it on their computer and allow it to access the local computer.
使基于 chrome 的 JavaScript 或特权 JavaScript 能够做更多事情的主要因素是用户必须在他们的计算机上明确安装它并允许它访问本地计算机。
Lastly, there is server-side JavaScript, such as Rhino or NodeJS, which are designed to run on the server. While they cannot read/write to a client side user's file system, they of course can read and write to the server's file system.
最后,还有服务器端 JavaScript,例如 Rhino 或 NodeJS,它们旨在在服务器上运行。虽然他们不能读/写客户端用户的文件系统,但他们当然可以读和写服务器的文件系统。
回答by verisimilitude
Use node.js http://nodejs.org/docs/v0.4.8/api/fs.html#file_SystemOr HTML 5 http://www.html5rocks.com/en/tutorials/file/dndfiles/(for reading files)
使用 node.js http://nodejs.org/docs/v0.4.8/api/fs.html#file_System或 HTML 5 http://www.html5rocks.com/en/tutorials/file/dndfiles/(用于读取文件)