使用 jQuery 读取/写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/582268/
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
Read/write to file using jQuery
提问by menardmam
Is there a way to get jQuery to get information to and from a file? Is it possible? How?
有没有办法让jQuery从文件中获取信息?是否可以?如何?
回答by Paolo Bergantino
No, JavaScript doesn't have access to writing files as this would be a huge security risk to say the least. If you wanted to get/store information server-side, though, you can certainly make an Ajax call to a PHP/ASP/Python/etc. script that can then get/store the data in the server. If you meant store data on the client machine, this is impossible with JavaScript alone. I suspect Flash/Java may be able to, but I am not sure.
不,JavaScript 无权写入文件,因为这至少可以说是一个巨大的安全风险。但是,如果您想在服务器端获取/存储信息,您当然可以对 PHP/ASP/Python/等进行 Ajax 调用。然后可以在服务器中获取/存储数据的脚本。如果您的意思是将数据存储在客户端机器上,那么单独使用 JavaScript 是不可能的。我怀疑 Flash/Java 或许可以,但我不确定。
If you are only trying to store a small amount of information for an unreliable period of time regarding a specific user, I think you want Web Storage APIor cookies. I am not sure from your question what you are trying to accomplish, though.
如果您只是想在一段不可靠的时间内存储关于特定用户的少量信息,我认为您需要Web Storage API或 cookie。不过,从你的问题中我不确定你想要完成什么。
回答by ja.
Yes it is possible.
对的,这是可能的。
The technique is described below
该技术描述如下
回答by Javier
both HTML5 and Google Gears add local storage capabilities, mainly by an embedded SQLite API.
HTML5 和 Google Gears 都添加了本地存储功能,主要是通过嵌入式 SQLite API。
回答by Eric Leschinski
Use javascript's execCommand('SaveAs', false, filename);
functionality
使用 javascript 的execCommand('SaveAs', false, filename);
功能
Edit: No longer works. This Javascript function used to work across all browsers, but now only on IE, due to browser security considerations. It presented a "Save As" Dialog to the user who runs this function through their browser, the user presses OK and the file is saved by javascript on the server side.
编辑:不再有效。此 Javascript 函数过去适用于所有浏览器,但出于浏览器安全考虑,现在仅适用于 IE。它向通过浏览器运行此功能的用户显示一个“另存为”对话框,用户按“确定”,文件由服务器端的 javascript 保存。
Now this code is an rare antique zero day collectible.
现在这个代码是一个罕见的古董零日收藏品。
// content is the data (a string) you'll write to file.
// filename is a string filename to write to on server side.
// This function uses iFrame as a buffer, it fills it up with your content
// and prompts the user to save it out.
function save_content_to_file(content, filename){
var dlg = false;
with(document){
ir=createElement('iframe');
ir.id='ifr';
ir.location='about.blank';
ir.style.display='none';
body.appendChild(ir);
with(getElementById('ifr').contentWindow.document){
open("text/plain", "replace");
charset = "utf-8";
write(content);
close();
document.charset = "utf-8";
dlg = execCommand('SaveAs', false, filename);
}
body.removeChild(ir);
}
return dlg;
}
Invoke the function like this:
像这样调用函数:
msg = "I am the president of tautology club.";
save_content_to_file(msg, "C:\test");
回答by jonstjohn
You will need to handle your file access through web programming language, such as PHP or ASP.net.
您将需要通过 Web 编程语言(例如 PHP 或 ASP.net)处理文件访问。
To set this up, you would:
要设置它,您将:
Create a script that handles the file reading and writing. This should be visible to the browser.
Send jQuery ajax requests to that script that either write data or read data. You would need to pass all of your read/write information through the request parameters. You can learn more about this in the jQuery ajax documentation.
创建处理文件读写的脚本。这应该对浏览器可见。
将 jQuery ajax 请求发送到写入数据或读取数据的脚本。您需要通过请求参数传递所有读/写信息。您可以在 jQuery ajax 文档中了解更多相关信息。
Make sure that you sanitize any data that you are storing, since this could potentially be a security risk. However, this is really just standard flat-file data storage, and is not necessarily that unusual.
确保清理存储的所有数据,因为这可能存在安全风险。然而,这实际上只是标准的平面文件数据存储,并不一定那么不寻常。
As Paolo pointed out, there is no way to directly read/write to a file through jQuery or any other type of javascript.
正如 Paolo 指出的那样,无法通过 jQuery 或任何其他类型的 javascript 直接读取/写入文件。
回答by Clyde
Cookies are your best bet. Look for the jquery cookie plugin.
饼干是你最好的选择。寻找 jquery cookie 插件。
Cookies are designed for this sort of situation -- you want to keep some information about this client on client side. Just be aware that cookies are passed back and forth on every web request so you can't store large amounts of data in there. But just a simple answer to a question should be fine.
Cookie 是为这种情况设计的——您希望在客户端保留有关此客户端的一些信息。请注意,cookie 会在每个 Web 请求中来回传递,因此您无法在其中存储大量数据。但是对问题的简单回答应该没问题。
回答by Josh Stodola
If you want to do this without a bunch of server-side processing within the page, it might be a feasible idea to blow the text value into a hidden field (using PHP). Then you can use jQuery to process the hidden field value.
如果您想在页面内不进行大量服务器端处理的情况下执行此操作,则将文本值吹入隐藏字段(使用 PHP)可能是一个可行的想法。然后就可以使用 jQuery 来处理隐藏字段的值了。
Whatever floats your boat :)
无论什么漂浮在你的船上:)