How can I create a file for storage on the client side with JavaScript?

你说的曾经没有我的故事 提交于 2019-11-26 18:29:45

问题


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?


回答1:


You have a few options:

  • Cookies
  • localStorage
  • database

Check this link:

  • HTML5: Client-side Storage

Creating a file is possible only in IE using ActiveX objects.




回答2:


If you want to store user settings, you should:

  1. use cookies
  2. store client information on the server

The ability for a webpage to access an individual's hard disk would be hazardous. However, as Trey pointed out below, you can use:

  • HTML 5 Client Side Storage (browser support still limited)
  • ActiveX/FileSystemObject (Windows/IE only)



回答3:


If you can live with the user having to actively store the file, Downloadify allows you to generate a client side "download" on the fly.




回答4:


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.

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!!




回答5:


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/




回答6:


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

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




回答7:


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.



来源:https://stackoverflow.com/questions/3950131/how-can-i-create-a-file-for-storage-on-the-client-side-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!