Page 1 of 4 123 ... LastLast
Results 1 to 10 of 38

Thread: Files in JavaScript ...

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Files in JavaScript ...

    Hello ,

    I was testing out a file opening writing and reading script that i made by myself and only the reading part does not work (for those of you dont know, javascript can create read and write files on a users computer and it can write it anywhere but it requires ActiveX) -

    <html>
    <body bgcolor="black" text="yellow">
    <div align="center">
    <input type="button" onclick="writef()" value="Create File"><br><br>
    <input type="button" onclick="readf()" value="Read File"><br>
    <textarea id="readfvalue" readonly style="width: 500px; height: 500px"></textarea>
    <script>
    function writef() {
    var fso = new ActiveXObject("Scripting.FileSystemObject")
    var fileo = fso.CreateTextFile("c:\\Documents and Settings\\pcbrainbuster\\Desktop\\text.txt", 1)
    fileo.WriteLine("Hello I Like Eating Fruit Usually...")
    fileo.WriteBlankLines(2)
    fileo.WriteLine("Actually Always \:\)")
    fileo.Close()
    }

    function readf() {
    var fsob = new ActiveXObject("Scripting.FileSystemObject")
    var fileob = fsob.CreateTextFile("c:\\Documents and Settings\\pcbrainbuster\\Desktop\\text.txt", 1)
    document.getElementById('readfvalue').value=fileob.ReadAll()
    fileob.Close()
    }
    </script>
    </div>
    </body>
    </html>

    Any ideas ?

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    From a web page point of view you can use the Activex technology for reading and writing files but this method has its own disadvantage it will work only on Microsoft Internet Explorer.

    Normally this is a security risk (I mean trying to read and write files in your users machine) so most of the browsers doesn't support this feature.

  3. #3
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yeah thats true, it brings up a message that asks you if you are sure ...

    BUt it has its own advantages too -
    - Can write, read, move, delete and create files and folders in any directory

    You can alos do so much more with this than cookies even, files like this are NOT cookies so they won't be deleted when the users delets them. They can be put ANYWHERE in the hard-drive, then you can read its value with ifs and elses and then create a great page with this...

    What I need to know is -
    1) What is wrongs with the code in the first post
    2) What else can Activex do

    Thanks

  4. #4
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Well heres something

    I think the problem lies with trying to create a file again not reading it in the read function.

    You wrote
    Code:
    function readf() {
    var fsob = new ActiveXObject("Scripting.FileSystemObject")
    var fileob = fsob.CreateTextFile("c:\\Documents and Settings\\pcbrainbuster\\Desktop\\text.txt", 1)
    document.getElementById('readfvalue').value=fileob.ReadAll()
    fileob.Close()
    }
    Line 3 says to create a file! - you want to read a file. So you need something like
    Code:
    fileob  = fsob.OpenTextFile("c:\\Documents and Settings\\pcbrainbuster\\Desktop\\text.txt").ReadAll();
    Heres my ActiveX code I use. I wrote them myself (Probably after seeing someone elses).
    Code:
    //////////ACTIVEX SCRIPTING////////////
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f=''
    
    //create a file
    function createFile(fileName, fileExt, fileLocation)
    	{
    	if(!fileName){return false}
    	if(!fileExt){var fileExt = ".txt"}
    	if(!fileLocation){var fileLocation = "" }//relative path
    	
    	fileURL = fileLocation+fileName+fileExt;
    	
    	if(!fso.FileExists(fileURL))
    		{
    		return null
    		}
    	else
    		{
    		f = fso.CreateTextFile(fileLocation+fileName+fileExt, false); // CreateTextFile(file_location [, over_write])  over_write = {true/false}
    		}
    		
    	f.Close();
    	}
    
    //return all content of a file if found
    function returnFileContents(fileName, fileExt, fileLocation)
    	{
    	if(!fileName){return false}
    	if(!fileExt){var fileExt = ".txt"}
    	if(!fileLocation){var fileLocation = "" }//relative path
    	
    	fileURL = fileLocation+fileName+fileExt;
    	
    	if(!fso.FileExists(fileURL))
    		{
    		return null
    		}
    	else
    		{
    		f = fso.OpenTextFile(fileURL).ReadAll();
    		f.Close();
    		return f
    		}
    	}
    	
    //alter contents of file
    function alterContentOfFile(fileName, fileExt, fileLocation, toDo, fileContents)//accepts strings and arrays as content
    	{
    	if(!fileName){return false}
    	if(!fileExt){var fileExt = ".txt"}
    	if(!fileLocation){var fileLocation = "" }//relative path
    	
    	fileURL = fileLocation+fileName+fileExt;
    	
    	if(toDo == "delete")
    		{
    		createFile(fileName, fileExt, fileLocation)
    		}
    	if(toDo == "add")
    		{
    		f = fso.OpenTextFile(fileURL);
    		f.WriteLine(fileContents)
    		f.Close();
    		}
    	}
    
    //alert(eventz)
    //////////////END ACTIVEX SCRIPTING//////////////
    Last edited by Bob90; 03-23-2007 at 01:50 PM. Reason: wrong info

  5. #5
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your post dude , i did not even know i did that , ok i will now edit my code and test it then will check out urs

  6. #6
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks IT WORKED !!!

    But there is still one problem , the writing part of the script adds two blank lines to the txt file but when i read it instead of being like this -

    Hello I Like Eating Fruit Usually...


    Actually Always

    How it is supposed to be, it turns out like this in the textbox -

    Hello I Like Eating Fruit Usually... Actually Always

    And i can't seem to understand whats wrong, so your help would be appreciated

  7. #7
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Please help me (i am crying in real life )

  8. #8
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Huh?

    Just remove
    Code:
    fileo.WriteBlankLines(2)
    Thats what's adding the blank lines!


    I have updated my activeX file functions to make them work better
    Code:
    //////////ACTIVEX SCRIPTING////////////
    //create a file
    function createFile(fileName, fileExt, fileLocation, overWrite)
    	{
    	var fso = new ActiveXObject("Scripting.FileSystemObject");
    	
    	if(!fileName){return false;}
    	if(!fileExt){var fileExt = ".txt";}
    	if(!fileLocation){var fileLocation = "";}//relative path
    	if(!overWrite){var overWrite = false;}
    	
    	fileURL = fileLocation+fileName+fileExt;
    	
    	if((!fso.FileExists(fileURL))||(fso.FileExists(fileURL)&&overWrite))
    		{
    		var f = fso.CreateTextFile(fileURL, overWrite); // CreateTextFile(file_location [, over_write])  over_write = {true/false}
    		f.Close();
    		return true;
    		}
    	else
    		{
    		return null;
    		}
    		
    	fso = null;
    	f = null;
    	}
    
    //return all content of a file if found
    function returnFileContents(fileName, fileExt, fileLocation)
    	{
    	var fso = new ActiveXObject("Scripting.FileSystemObject");
    	
    	if(!fileName){return false}
    	if(!fileExt){var fileExt = ".txt"}
    	if(!fileLocation){var fileLocation = "";}//relative path
    	
    	fileURL = fileLocation+fileName+fileExt;
    	
    	if(!fso.FileExists(fileURL))
    		{
    		return null;
    		}
    	else
    		{
    		var f = fso.OpenTextFile(fileURL,1)
    		var txt = f.ReadAll();
    		f.Close();
    		return txt;
    		}
    	
    	fso = null;
    	f = null;
    	}
    	
    //alter contents of file
    function alterContentOfFile(fileName, fileExt, fileLocation, toDo, fileContents)//accepts strings and arrays as content
    	{
    	var fso = new ActiveXObject("Scripting.FileSystemObject");
    	
    	if(!fileName){return false}
    	if(!fileExt){var fileExt = ".txt"}
    	if(!fileLocation){var fileLocation = "";}//relative path
    	
    	fileURL = fileLocation+fileName+fileExt;
    	
    	if(fso.FileExists(fileURL))
    		{
    		if(toDo == "delete")
    			{
    			createFile(fileName, fileExt, fileLocation);
    			}
    		if(toDo == "add")
    			{
    			var f = fso.OpenTextFile(fileURL,8);
    			f.WriteLine(fileContents);
    			f.Close();
    			return true;
    			}
    		}
    		
    	fso = null;
    	f = null;
    	}
    
    //http://4umi.com/web/javascript/fileread.htm
    //////////////END ACTIVEX SCRIPTING//////////////
    Last edited by Bob90; 03-23-2007 at 05:40 PM. Reason: misquote

  9. #9
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    No no no You got it all wrong,

    Thats supposed to happen. In the textarea the blank lines from the text file do not show and i simply don't know whats wrong instead it shows the first and second part together , so the text file has this layout inside it -

    sentence 1
    blank
    blank
    sentence 2

    But the textarea thing shows it like this -

    sentence 1 sentence 2

    But i need it to be in the file's format.

    Thanks

  10. #10
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default it's an "oh it's so simple" moment!

    Just change .innerHTML to .value
    It seems to keep the formatting.
    Can't tell you why though.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •