The Magic function of the FSO in ASP-write files

Source: Internet
Author: User
FSO Author: Gan Ganping;


Suppose you want to create a simple guest book, you can build a database in which to store the user's information. However, if you don't need the power of the database, using the FSO to store the information will save you time and money. Also, some ISPs may limit database applications on the Web.

Suppose you collected some user information in a form, here is a simple form HTML code:

< html>
< body>

< form action= "formhandler.asp" method= "POST" >
< input type= "text" size= "ten" name= "username" >
< input type= "text" size= "ten" Name= "homepage" >
< input type= "text" size= "ten" Name= "Email" >
</form>
</body>

And look at the code that handles the form in formhandler.asp:

<%
' Get form info '
StrName = Request.Form ("username")
Strhomepage = Request.Form ("homepage")
Stremail = Request.Form ("Email")

' Create ' the FSO object
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")

So far, there's nothing new, just getting the value of a form field and assigning it to a variable. Here's the interesting part-write the file:

Path = "C:emp est.txt"
ForReading = 1, ForWriting = 2, ForAppending = 3

' Open ' file
Set file = Fso.opentextfile (path, ForAppending, TRUE)

' Write the info to the ' file
File.write (strName) & vbCrLf
File.write (strhomepage) & vbCrLf
File.write (stremail) & vbCrLf

' Close and clean up
File.close
Set file = Nothing
Set fso = Nothing


Recall that the OpenTextFile method returns a TextStream object, which is another object in the FSO model. The TextStream object reveals ways to manipulate the contents of a file, such as writing, reading, and skipping a line. The VB constant vbCrLf produces a newline character.

True is defined in the OpenTextFile command argument, which tells the system to create a file if it does not exist. If the file does not exist and the true parameter is not defined, an error occurs.

Now go to the directory c:emp, open test.txt, you can see the following information:

User ' s name
User ' s home page
User ' s Email

Of course, these words can be replaced by any content entered in the form.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.