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

前端 未结 7 772
独厮守ぢ
独厮守ぢ 2020-12-03 18:35

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?

相关标签:
7条回答
  • 2020-12-03 19:02

    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.

    0 讨论(0)
  • 2020-12-03 19:08

    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/

    0 讨论(0)
  • 2020-12-03 19:10

    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

    0 讨论(0)
  • 2020-12-03 19:14

    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)
    0 讨论(0)
  • 2020-12-03 19:15

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

    0 讨论(0)
  • 2020-12-03 19:18

    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.

    0 讨论(0)
提交回复
热议问题