Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to a text file using ASP

Tags:

asp-classic

Im totally new to ASP so any help would be very grateful. I have a html page which has a form for users to leave their details and when they submit it goes to a text file fine but what I want is once they have submitted it I want to have an alert saying "comment saved" and to stay on the original page so they can submit another if they choose but when the user submits it goes to a blank page. My Form is

<form method="post" action="comments.asp">
<br><br>
Age <input type="text" name="age" />
Name<textarea rows="1" cols="70" <input type="text" name="name" /></textarea> 
<input type="submit" value="Send Comment">
</form>

my comments.asp file code is

<%
Dim age, name
age = Request.Form("age")
name = Request.Form("name")
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile("C:\Inetpub\wwwroot\Wiki\Comments.txt",8,true)
f.WriteLine(age & " " & date() & " " & name)
f.Close
set f=nothing
set fs=nothing
%>
like image 207
New2Programming Avatar asked Sep 30 '22 14:09

New2Programming


2 Answers

<%
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'Open the file for writing
Set f = fso.CreateTextFile(s_path & "/" & s_file_being_created, True)
f.Write(m)
f.Close
Set f = Nothing
Set fso = Nothing
%>
like image 185
ScotterMonkey Avatar answered Oct 08 '22 16:10

ScotterMonkey


For those of you writing ASP with JScript instead of VBS, the code might look something like this:

var fileSystemObject = Server.CreateObject("Scripting.FileSystemObject");
var textStream = fileSystemObject.CreateTextFile(filePath, true, true);
textStream.Write(fileContent);
textStream.Close();
delete textStream;
delete fileSystemObject;

Documentation references:

  • Scripting.FileSystemObject object
  • FileSystemObject.CreateTextFile() method
  • TextStream object
  • Write() method
  • Close() method
  • delete operator
like image 35
ˈvɔlə Avatar answered Oct 08 '22 16:10

ˈvɔlə