GajendraSinghParihar
GajendraSinghParihar

Reputation: 9131

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

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?

Upvotes: 11

Views: 28175

Answers (7)

abbasdgr8
abbasdgr8

Reputation: 555

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

Upvotes: 3

Flimm
Flimm

Reputation: 150643

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.

Upvotes: 0

Cool.Priya
Cool.Priya

Reputation: 56

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/

Upvotes: 1

Pekka
Pekka

Reputation: 449425

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.

Upvotes: 2

vol7ron
vol7ron

Reputation: 42109

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:

Upvotes: 2

zod
zod

Reputation: 12417

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

Upvotes: 0

BrunoLM
BrunoLM

Reputation: 100331

You have a few options:

  • Cookies
  • localStorage
  • database

Check this link:

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

Upvotes: 6

Related Questions