PDA

View Full Version : Writing / Creating a text file with ActiveX


wonko
04-06-2008, 09:41 AM
Hello, this is my first endeavor into using javascript and AJAX. Right now I'm just trying to create and write to a text file. Here is my code but I cannot get it to do either of these things:

<script language="JavaScript">

var filename = "data.txt";
var name = "test";
var password = "testpassword";

var fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FileExists(filename))
{
file = fso.OpenTextFile(filename, ForAppending, false);
file.WriteLine(name);
file.WriteLine(password);
}
else
{
var file = fso.CreateTextFile(filename, true);
file.WriteLine(name);
file.WriteLine(password);
}
file.Close();


</script>

Any idea what I am doing wrong?

rnd me
04-06-2008, 11:11 AM
function IEsave(filename, sData) {
var fso, a;
fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.CreateTextFile(filename, 2, false);
file.write(sData);
file.Close();
return file;
}

wonko
04-06-2008, 08:02 PM
Alright, so in order to create a file named "data.txt" this code should work right?


var fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.CreateTextFile('data.txt', 2, false);
file.Close();


and in order to run it I just open the document in a web browser?

rnd me
04-09-2008, 05:47 AM
save as test.htm:
<script>
function IEsave(filename, sData) {
var fso, a;
fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.CreateTextFile(filename, 2, false);
file.write(sData);
file.Close();
return file;
}

alert(IEsave("data.txt", ""));

</script>

A1ien51
04-09-2008, 03:05 PM
FYI: Ajax has nothing to do with writing a file to the user's computer. It deals with talking with the server with the XMLHttpRequest Object.

Eric

wonko
04-12-2008, 01:21 AM
Ah thanks I finally figured this out ><. I now use the HMLHttpRequest to call a PHP script that does the filework for me

mjlorbet
04-12-2008, 08:48 AM
mmm, that depends on where you're trying to drop the file, server or client & where you're trying to get the info from for the file, there are different ways of going about this but ActiveX and the Scripting.FileSystemObject just seem like horrible options because of the number of security settings and/or hassle to the end user that these could cause. you may just be better off doing something like this (assuming that your file you're trying to write is a report of some sort based off of a/some database record(s))

1) write a php page that accepts a query string parameter for the basis of the report
2) have the php page use headers specifying the file name you wish to write and set the content type to bad/type (so that the browser will hopefully not just attempt to render the content)
3) use window.open() to spawn the request to grab the file, attach an onload event to it to close the window

if you're just looking for a log file then the way you've figured out is probably the way to go