Click to See Complete Forum and Search --> : create text file problem


chicken.king
03-29-2005, 05:08 AM
hello i'm new in web base programming. I have a problem to ask. Any answer will be appreciate. I had written the program that created a new textfile. But why the FOLDER TEST.doc file is already create automatically ?? Beside this, the "CREATE button" that i click can't create the text file. All i want is when i click the button the text file will be created Here is the program :-

<html>
<head>
<script language ="vbscript">
function file ()
<%
dim filesys, filetxt, getname, path , create

Set filesys = server.CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile( "c:\FOLDER TEST.doc",true)
path = filesys.GetAbsolutePathName("c:\FOLDER TEST.doc")
getname = filesys.GetFileName(path)

filetxt.WriteLine("EPS SYSTEM TEST.")
filetxt.Close

%>

end function
</script>

</head>
<body>
<form>
<input type="button" id="create" value="CREATE" onclick="file()" />
</form>
</body>
</html>

buntine
03-29-2005, 05:19 AM
Welcome to the forums. ;)

Your logic is set out incorrectly. You have ASP within client-side VBScript. The two technologies need to be treated differently and presented seperately.

ASP objects (such as the FileSystemObject) cannot be instantiated from the client. You are mixing up ASP and VBScript.

The function will need to be set out similar to this:

<%
function file ()
dim filesys, filetxt, getname, path , create

Set filesys = server.CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile( "c:\FOLDER TEST.doc",true)
path = filesys.GetAbsolutePathName("c:\FOLDER TEST.doc")
getname = filesys.GetFileName(path)

filetxt.WriteLine("EPS SYSTEM TEST.")
filetxt.Close
end function
%>

You will not need to place it within <script> tags.

You cannot call the method from the client via the onclick event. You will need to have the page reloaded so the function can be executed from the server. Like this:

<form action="<%= Request.ServerVariables("SCRIPT_NAME") %>" method="get">
<input type="submit" value="CREATE" />
<input type="hidden" name="action" value="create" />
</form>

This will redirect you to the same page but the URL will contain a parameter named action. The end of the URL will look like: /fileName.asp?action=create

You can have the file() function called from the server when this action is apparent.

If Request.QueryString("action") = "create" Then
file()
End If

It may seem overly difficult, but it is not once you get started.

Regards.

phpnovice
03-29-2005, 04:44 PM
Actually, you can instantiate the FileSystemObject on the client (I've done it) -- it is just a security nightmare if creating a file on the client is what you actually desire.

chicken.king
03-29-2005, 09:53 PM
THANKS A LOT IT REALLY WORKS
I AM VERY APPRECIATE IT.

Another thing that I would like to ask is, when I refresh the web the document file is created. How can I prevent this to do so ?

buntine
03-29-2005, 11:17 PM
You just need to edit your file() function a little bit so that it only creates the file if it does not already exist.

function file ()
dim filesys, filetxt, getname, path , create

Set filesys = server.CreateObject("Scripting.FileSystemObject")

If filesys.fileExists("c:\FOLDER TEST.doc") Then
exit function
End If

Set filetxt = filesys.CreateTextFile( "c:\FOLDER TEST.doc",true)
path = filesys.GetAbsolutePathName("c:\FOLDER TEST.doc")
getname = filesys.GetFileName(path)

filetxt.WriteLine("EPS SYSTEM TEST.")
filetxt.Close
end function

Regards.

chicken.king
03-29-2005, 11:41 PM
No what I mean is, I don't want the file to be create when I refresh the web

buntine
03-29-2005, 11:46 PM
I don't understand...

You want it to be created once, but then not created afdter that?

chicken.king
03-29-2005, 11:56 PM
what I mean is why the file is create automatically when i refresh the web page

buntine
03-30-2005, 12:07 AM
When do you want it to be created? Just get rid of the code if you never want it to be created again.

You could use the HTTP_REFERER server variable to make sure the previous page was different.

Dim strCurrentPage, strPreviousPage
With Request
strCurrentPage = .serverVariables("SCRIPT_NAME")
strPreviousPage = .serverVariables("HTTP_REFERER")
End With

From there, you can do a simple check to see whether the two are different.

Regards.

chicken.king
03-30-2005, 12:17 AM
thank you so much
i know how to do it
I did learn a lot from you

buntine
03-30-2005, 12:18 AM
Your welcome. ;) I hope things work out for you.