如何使用 JavaScript 在客户端创建用于存储的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3950131/
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 can I create a file for storage on the client side with JavaScript?
提问by Champ
I need to create a temporary file to store user settings on the client side. Is it possible to create a simple log file using JavaScript?
我需要创建一个临时文件来存储客户端的用户设置。是否可以使用 JavaScript 创建一个简单的日志文件?
采纳答案by BrunoLM
You have a few options:
您有几个选择:
- Cookies
- localStorage
- database
- 饼干
- 本地存储
- 数据库
Check this link:
检查此链接:
Creating a file is possible only in IE using ActiveX objects.
只能在 IE 中使用 ActiveX 对象创建文件。
回答by vol7ron
If you want to store user settings, you should:
如果要存储用户设置,您应该:
- use cookies
- store client information on the server
- 使用cookies
- 在服务器上存储客户端信息
The ability for a webpage to access an individual's hard disk would be hazardous. However, as Trey pointed out below, you can use:
网页访问个人硬盘的能力将是危险的。但是,正如 Trey 在下面指出的那样,您可以使用:
- HTML 5 Client Side Storage(browser support still limited)
- ActiveX/FileSystemObject(Windows/IE only)
- HTML 5 客户端存储(浏览器支持仍然有限)
- ActiveX/FileSystemObject(仅限 Windows/IE)
回答by Pekka
If you can live with the user having to actively store the file, Downloadifyallows you to generate a client side "download" on the fly.
如果您可以忍受必须主动存储文件的用户,Downloadify允许您即时生成客户端“下载”。
回答by abbasdgr8
You cannot! This violates browser security protocols.
你不能!这违反了浏览器安全协议。
All client-side code in a browser (HTML/CSS/Java-Script) is supposed to get executed inside a security sandbox. As soon as you close the browser session, this sandbox is destroyed. This sandbox protects your local filesystem from malicious attacks.
浏览器中的所有客户端代码(HTML/CSS/Java-Script)都应该在安全沙箱中执行。一旦您关闭浏览器会话,这个沙箱就会被销毁。此沙箱可保护您的本地文件系统免受恶意攻击。
Ideally, if you were able to do this, then, by just browsing through several links, those sites should be able to write viruses on your system as you do so!!
理想情况下,如果您能够做到这一点,那么只需浏览几个链接,这些站点就应该能够像您一样在您的系统上编写病毒!!
回答by Cool.Priya
You can't create file on fly to the client side as there are Security restrictions
由于存在安全限制,您无法即时创建文件到客户端
but i found a nice article on file by JavaScript have a look http://www.nczonline.net/blog/2012/05/31/working-with-files-in-javascript-part-4-object-urls/
但我发现了一篇关于 JavaScript 文件的好文章,看看http://www.nczonline.net/blog/2012/05/31/working-with-files-in-javascript-part-4-object-urls/
回答by Flimm
A library that lets you create a plain text file (or an image file or a rich text file) on the client-side for download is FileSaver.js.
允许您在客户端创建纯文本文件(或图像文件或富文本文件)以供下载的库是FileSaver.js。
回答by zod
Try this anyway
试试这个
var fso = new ActiveXObject("Scripting.FileSystemObject");
varFileObject = fso.OpenTextFile("C:\Sachin.txt", 2, true,0); // 2=overwrite, true=create if not exist, 0 = ASCII
varFileObject.write("File handling in Javascript");
varFileObject.close();
http://www.codeproject.com/KB/scripting/JavaScript__File_Handling.aspx
http://www.codeproject.com/KB/scripting/JavaScript__File_Handling.aspx
But i dont think you have to do this type of experiments. You can create and do many file manipulations using server side languages.Thats better
但我不认为你必须做这种类型的实验。您可以使用服务器端语言创建和执行许多文件操作。那更好