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

本小妞迷上赌 提交于 2019-11-27 15:11:39

You have a few options:

  • Cookies
  • localStorage
  • database

Check this link:

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

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:

Pekka 웃

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.

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

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/

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

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.

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