Forum Moderators: open

Message Too Old, No Replies

Reading/Writing local files for makeshift databasing

         

Greven

8:37 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



Basically, I have very few resources available where I'm working on this, so I have to do some databasing with javascript. At this point, I made by hand a simple file, where the entries are seperated by "*" and the items inside of each entry is seperated by a ",". Using the following code I can read the file in no problem, and have been able to parse it out as I need it to.


function readfile(filename)
{
var string = "";

objXml = new ActiveXObject("MICROSOFT.XMLHTTP");

objXml.open("POST", filename, true);
objXml.onreadystatechange=function()
{
if (objXml.readyState==4)
{
string = objXml.responseText;
}
}
objXml.send(null);
return string;
}

I then use strarray = string.split(/\*/); to get an array of my items and elemarray = strarray[i].split(/\,/); to get an array of each item in the entry. It reads in fine, and I have no problem parsing it and populating a form with this information.

The problem comes when I try to read in a file that is saved. I pull the modified information from a form, put it into a big string, adding the * and ' in the right place, and then save it to the same file using this code:

function write_file(string, filename)
{
SaveFrame.document.open("text/html","replace")
SaveFrame.document.write(string)
SaveFrame.document.close()
SaveFrame.focus()
SaveFrame.document.execCommand('SaveAs',false,filename)
}

Where SaveFrame is just a non-displayed iframe. I looks like it writes all the infomation in fine, when I examine the file everything is there, all the seperators are in the right place, noting wrong with it at all.

However, when I try to read it in the saved file rather than the one I made, it only returns 1 entry with one array with 1 character in it: S. S happens to be the first character of the file, so I tried changing it, having made backups of the original databases, and it always returns the first character only of the file.

The only thing I can come up with is that when it asks me to save it, it defaults to Unicode. When saved in this method, I get the aboce results. When saved in "user defined", for example, it adds a whole bunch of extra data, but it reads the entire file. Maybe someone can explain whats going on. Thanks in advance.

Greven

8:44 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



I guess that I should clarify that I'm using IE 6.0.2800

Greven

1:26 pm on Apr 10, 2005 (gmt 0)

10+ Year Member



Well, I tried something a little different and saved a backup of the files to the same location as unicode, then unicode big endian(?) and then UTF-8, and all are read in again. So used my code to create the file, and it wouldn't read it in. I loaded the file in notepad, saved in notepad, and tried to re-read the exact same uneditec content, works no problem, is read in correctly. Does maybe the IE execCommand save in a different format from something like notepad for a standard text file?

BTW, this is for intranet use, so IE 6 specific solutions are just fine, its the only browser that will use this code. Thanks again.

Greven

5:39 pm on Apr 10, 2005 (gmt 0)

10+ Year Member



Well, I got it working. Basically, it was a matter of specifying what type of encoding to use, as me read method did not like the unicode character set that it was being saved as. I added this:


SaveFrame.document.charset="UTF-8";

And it saves and then reads in nicely. Hope this helps someone else at some point.

Bernard Marx

10:14 am on Apr 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting. Thanks.