JavaScript to create and read local files (ie & Firefox)

Source: Internet
Author: User

In IE, activexobject can be used for implementation.

VaR FSO = new activexobject ("scripting. fileSystemObject "); var F1 = FSO. createtextfile ("test.csv", true); f1.writeline ("hello"); f1.writeblanklines (1); // wrap f1.close ();

The relative address can be used here. If you want to know the absolute path of the file, you can use

      var ofile=fso.getFile("test.csv");      alert(ofile.path);

Activexobject cannot be used in Firefox. You can implement it in the following ways:

var path = "c:\\test.csv";var content = "Hell0";try {    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");} catch (e) {   alert("no permisson..."); } var file = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsILocalFile);file.initWithPath(path); if ( file.exists() == false ){   file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 ); } var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"] .createInstance( Components.interfaces.nsIFileOutputStream );outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 ); var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"] .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);converter.charset = 'UTF-8'; var convSource = converter.ConvertFromUnicode(content);var result = outputStream.write( convSource, convSource.length ); outputStream.close();alert("File was saved in "+path);

If the no permisson... window pops up, modify the Firefox config.

Modify method: in the address bar, enter about: config --> Find signed. Applets. codebase_principal_support --> double-click to set the value to true.

The path here specifies an absolute address. If you are interested in using a relative address, you can study it again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.