Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 14 people (71%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I create a file, write to a file and read from a file from a web page using JavaScript?

Apr 8th, 2008 18:46
ha mo, Abolfazl Shirazi, Knud van Eeden, katia,


Abolfazl Shirazi :
It seems that you may be able to read files data using javascript but 
I think changing them is impossible!!!
---
----------------------------------------------------------------------
--- Knud van Eeden --- 23 August 2006 - 11:37 pm ---------------------
JavaScript: File: How to write or read to a file? [load / save]
Method: Using 'IFRAME': Write to a file
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<IFRAME
  ID="SaveFrame"
  STYLE="display:none"
>
</IFRAME>
<!-------------------------------------------------------------------->
<SCRIPT LANGUAGE="JavaScript">
 function PROCHtmlSave() {
  SaveFrame.document.open( "text/html", "replace" );
  SaveFrame.document.write( "<HTML>" );
  SaveFrame.document.write( "<BODY>" );
  SaveFrame.document.write( "test" );
  SaveFrame.document.write( "knud" );
  SaveFrame.document.write( "</BODY>" );
  SaveFrame.document.write( "<HTML>" );
  SaveFrame.document.close();
  SaveFrame.document.focus();
  SaveFrame.document.execCommand( 'SaveAs', null, 'myfilename.html' );
 }
</SCRIPT>
<!-------------------------------------------------------------------->
[ <A HREF="javascript:PROCHtmlSave()">Save as</a> ]
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
===
Method: Using ActiveX object: Write the source of current page to file
Example:
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<INPUT
  TYPE = "button"
  NAME = "button1"
  VALUE = "click to save this page"
  ONCLICK = "document.all.WebBrowser.ExecWB( 4, 1 )"
/>
<!-------------------------------------------------------------------->
<OBJECT
  CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"
  HEIGHT="0"
  ID="WebBrowser"
  WIDTH="0"
/>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
===
Method: Using ActiveX object: Write to or read from a file
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
function FNTextStreamTestS() {
 var fso;
 var f;
 var filenameS = "";
 var ts;
 var s;
 var ForReading = 1;
 var ForWriting = 2;
 var ForAppending = 8;
 var TristateUseDefault = -2
 var TristateTrue = -1
 var TristateFalse = 0;
 fso = new ActiveXObject( "Scripting.FileSystemObject" );
 filenameS = document.form1.file1.value;
 fso.CreateTextFile( filenameS );
 f = fso.GetFile( filenameS );
 ts = f.OpenAsTextStream( ForWriting, TristateUseDefault );
 ts.Write( "Hello World" );
 ts.Close();
 ts = f.OpenAsTextStream( ForReading, TristateUseDefault );
 s = ts.ReadLine();
 ts.Close();
 return( s );
}
<!-------------------------------------------------------------------->
</SCRIPT>
<!-------------------------------------------------------------------->
<FORM
  NAME="form1"
 >
<!-------------------------------------------------------------------->
<INPUT
  NAME="file1"
  TYPE="file"
>
</INPUT>
<!-------------------------------------------------------------------->
<BR/>
<!-------------------------------------------------------------------->
<BUTTON
  NAME="button1"
  ONCLICK = ' ;
   alert( FNTextStreamTestS() );
  '
>
View content of filename
</BUTTON>
<!-------------------------------------------------------------------->
</FORM>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
===
Using an input box for a filename also
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
<!-------------------------------------------------------------------->
function FNTextStreamTestS() {
 var fso;
 var f;
 var filenameS = "";
 var ts;
 var s;
 var ForReading = 1;
 var ForWriting = 2;
 var ForAppending = 8;
 var TristateUseDefault = -2
 var TristateTrue = -1
 var TristateFalse = 0;
 fso = new ActiveXObject( "Scripting.FileSystemObject" );
 filenameS = document.form1.file1.value;
 fso.CreateTextFile( filenameS );
 f = fso.GetFile( filenameS );
 ts = f.OpenAsTextStream( ForWriting, TristateUseDefault );
 ts.Write( "Hello World" );
 ts.Close();
 ts = f.OpenAsTextStream( ForReading, TristateUseDefault );
 s = ts.ReadLine();
 ts.Close();
 return( s );
}
<!-------------------------------------------------------------------->
</SCRIPT>
<!-------------------------------------------------------------------->
<FORM
  NAME="form1"
 >
<!-------------------------------------------------------------------->
<INPUT
  NAME="file1"
  TYPE="file"
>
</INPUT>
<!-------------------------------------------------------------------->
<BR/>
<!-------------------------------------------------------------------->
<BUTTON
  NAME="button1"
  ONCLICK = '
   alert( FNTextStreamTestS() );
  '
>
Write text to new (fill in) or existing (browse) filename and read it 
again
</BUTTON>
<!-------------------------------------------------------------------->
</FORM>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
===
Tested successfully on
Microsoft Windows XP Professional (service pack 2),
running
Microsoft Internet Explorer v6
===
Internet: see also:
HTML: Input: Box: Filename: Create: How to create a filename input in 
HTML?
http://www.faqts.com/knowledge_base/view.phtml/aid/31258/fid/1154
----------------------------------------------------------------------
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com