Jump to content

writing text files with asp


ecluley

Recommended Posts

I am learning ASP whilst building a website to allow users to login and edit text files which are part of ofther websites i make for them. I have 3 pages: index.asp which is a simple login form with code for checking if username and password is correct. This creates a session object which stores the username and a string "YES" if the password is valid and "NO" if not. This all works perfectly. The second page is a page to edit the text file. It reads the seession object and then displays the text contained in the text file. It will also display a form for enterning new text into the file. Every user will have a different folder on the server named identically to their username. So for example, username "umb01" will have a folder called "umb01" containg a file called news.txt. This page also works fine, except when u click on the submit button on the form. It takes you to the page upload_news.asp which is where the problems come in. This page should simply write the data to the file but it return an error.Heres the codeindex.asp:

<html><head><%sub refresh()	dim fname, password, usrdata(2), pswrddata(2), i, j	fname=Request.form("fname")	password=Request.form("pswrd")	If fname<>"" Then		usrdata(0)="umb01"		usrdata(1)="umb02"		usrdata(2)="umb03"		pswrddata(0) ="cheese"		pswrddata(1) ="beef"		pswrddata(2) = "pedo"		j=200		for i = 0 to 2			If usrdata(i) = fname Then				j = i				response.write("Usr names match, checking passwords <br/>")			End If		next		If j<=199 Then			If password=pswrddata(j) Then				response.write("passwords match <br/> Access Granted")				Session("permission")="YES" 				Session("username")=fname 				Response.Redirect("ftp.asp")				Response.End()			Else				response.write("Access Denied")				Session("permission")="NO" 				Session("username")=fname 				Response.Redirect("ftp.asp")				Response.End()			End If		End If	End Ifend sub%><body><form action="index.asp" method="post">Your name: <input type="text" name="fname" size="20" />Your pswrd: <input type="password" name="pswrd" size="20" /><input type="submit" value="Submit" /></form><%call refresh()%></body></html>

ftp.asp:

<html> <title>Our private pages</title> <body><%If Session("permission") = "YES" Then	Response.write("Access Granted")%>			Current Text:<br>			<%				Dim fs,f, usr, newsTxt, ts, fo				usr = Session("username")				newsTxt = request.form("news")				Set fs=Server.CreateObject("Scripting.FileSystemObject")				Set f=fs.OpenTextFile(Server.MapPath(usr & "\news.txt"), 1)				Response.write(f.ReadAll)				set f=nothing				set fs=nothing			%>			<FORM ACTION="upload_news.asp" METHOD=post> 			News: <input type="textarea" name="news" size="20" />			<INPUT TYPE=submit value="Submit"> 			</FORM>)<% Else	Response.write("Access Denied")End If%></body> </html>

upload_news.asp:

<html> <title>Edit News Page</title> <body><%Dim fs,f, usr, newsTxtusr = Session("username")newsTxt = request.form("news")Set fs=Server.CreateObject("Scripting.FileSystemObject")Set f = fs.CreateTextFile(Server.MapPath(usr & "\news.txt"))f.write(newsTxt)set f=nothingset fs=nothing%></body> </html>

The problem line seams to be this one:Set f = fs.CreateTextFile(Server.MapPath(usr & "\news.txt"))but i don't know what ive done wrong.If anyone has any suggestions / comments / ideas, please help! I am running windows vista, so thats... IIS 7.0 i think.Thank you all.Ewen

Link to comment
Share on other sites

"An error occurred on the server when processing the URL. Please contact the system administrator"it stops happening when i remark out the offending line of code but i kinda need that line of code...when i have "show friendly http error messages" ticked in internet options it gives me a HTTP 500 Internal Server Errorthanks for the responseEwen

Link to comment
Share on other sites

You'll need to find out what the specific error message is in order to determine what the problem is. Try using another browser like Firefox or Opera, they will show the error message if it gets sent. The server might be using a custom error page, and if that's the case then you might need to ask whoever is running the server how you can determine what the error in your script is.Since the line is where you create a text file, you need to make sure that you have permission to create the text file, and you might also want to check that the file does not exist before you try to create it. Print out the full filename that you are giving to the CreateTextFile method to make sure that the path is correct, and also make sure that the user that ASP runs under, probably called IUSR_<machine name>, has permission to write to the destination directory.

Link to comment
Share on other sites

Okay, the text file DOES already exist so i assume this is the problem. How can I make ASP overwrite the existing text file? I want to be able to let the user make changes to the existing text file. how do i do this? thanks.

Link to comment
Share on other sites

Okay, the text file DOES already exist so i assume this is the problem. How can I make ASP overwrite the existing text file? I want to be able to let the user make changes to the existing text file. how do i do this? thanks.
Check out this page for information on how to use CreateTextFile to allow it to overwrite existing files:http://www.w3schools.com/asp/met_createtex..._filesystem.aspHowever, you're probably better off using OpenTextFile to attempt to open the file. There is an optional parameter in that function that will create the file if it doesn't already exist.http://www.w3schools.com/asp/met_opentextfile.asp
Link to comment
Share on other sites

ok, i have tried the openTextFile method and am now running it though firefox. i have uploaded it to a free asp server and am running it from there as I have changed computers (on holiday) and no am without ISS (dam xp home!) The error message i recieve isServer object error 'ASP 0177 : 800401f3'Server.CreateObject Failed/umbrellamembers/upload_news.asp, line 11800401f3 i now recieve this for the ftp.asp page aswell when i try to read the text file.both pages, ftp.asp and upload_news.asp seam to have a problem with Server.CreateObject. This line normally works fine when i run it though IIS on my computer but now i have uploaded it it causes problems.Sory about all this but i just dont understand why i wont work and will on my pc at home but not when uploaded to the webspace. Thanks for all your help thus far,Ewen

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...