Deep understanding of the magical features of the FSO in ASP

Source: Internet
Author: User
Tags error handling file system html form log readline
FSO in the ASP, the FSO means the file system object, which is the filesystem objects.

We are going to manipulate the computer file system, which is located above the Web server. So, make sure you have the right permissions for this. Ideally, you can set up a Web server on your own machine so that you can easily test it. If you are running on a Windows platform, try Microsoft's free personal Web server PWS.

FSO Model Object

Drive object: Drive objects for access to disks or network drives
FileSystemObject object: File system objects for accessing a computer's file system
Folder object: Folders objects for accessing all properties of a folder
TextStream object: Text Stream objects for accessing file contents

You can use the object above to do anything on the computer, including sabotage;-( Therefore, please use the FSO carefully. In a Web environment, storing information is very important, such as user information, log files, and so on. The FSO provides a powerful and easy way to save data efficiently. In this article, we focus on FileSystemObject and TextStream objects.

FSO is supported by Microsoft and presumably no longer uses ASP for non-Windows systems.

How to use the FSO?

To perform all of the work using the FSO, you first create the object, as in the following code:

% Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
% >

This creates the FSO and assigns the variable FSO, and then you can use the familiar Object.Method syntax to perform file system operations (view the Visual Basic documentation for more information on object and Object Wizard programming). Here we can use Fso.method or Fso.property, which will be seen in the following example.

The FSO model is located in the Script runtime DLL file provided by Microsoft, which is scrrun.dll. You can refer to this DLL file in any application, such as Ms Access,word. That is, it is not limited to applying it in ASP.

Here is a brief list of FSO methods:

Fso method
CopyFile copy one or more files to a new path
CreateTextFile creates a file and returns a TextStream object
DeleteFile Delete a file
OpenTextFile opens the file and returns the TextStream object to read or append

For full FSO methods and properties, check out Microsoft MSDN. Let's look at a few examples.

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=" name= "username"
"Input type=" text "size=" name= "homepage"
"Input type=" text "size=" name= "Email" "
/form>
/body>
/html>

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" 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 the" 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.

Now there are some user information saved in the file, just like a simple database. Suppose a user wants to know all the visitors, detach the relevant part from the registered information, because there is no structured column like a database.

We know that in the file created, line 1th is user name, line 2nd is their home page, and line 3rd is their e-mail address. Subsequently registered users also store their information according to such a structure, so each 3 row will contain a user's registration information. Knowing this, you can write the following code to display the information:

%
"Create" FSO Object
Set fso = Server.CreateObject ("Scripting.FileSystemObject")
Path = "C:emp est.txt"

"Open the" file
Set file = Fso.opentextfile (path, 1)--for
Reading

Next, analyze each row and format the data:

Do until file. AtEndOfStream
Response.Write ("Name:" & file. ReadLine & "")
Response.Write ("Home Page:" & file.) ReadLine & "")
Response.Write ("Email:" & file. ReadLine & "p>")
Loop

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

There's only a very simple output here, but you can include tables or DHTML form information depending on the situation.

If the file has been correctly created and written, the small loop above will properly list the information for each person in the database. The ReadLine method reads 1 lines of content until a newline character is encountered, and subsequent ReadLine calls will read the next line. AtEndOfStream is the property of the TextStream object, which tells us when the end of the file is encountered.

Suppose, for some reason, we did not form the file correctly, if a user had only 2 lines of information instead of 3 lines, there would be some errors. Our loop here retrieves the next 3 lines of information from the file, and if there are no more than 3 rows of information, the following error message appears:

Server object error ' ASP 0177:800a003e '
  
Therefore, be sure to add some error handling code to prevent the file from inserting extra rows or missing the necessary line information.

The basics are discussed, and then there is the question of permission permission. The FSO is run to create its user account permissions, in other words, if someone accesses your page from the Internet, the Internet account creates the FSO. If you log on to the computer as an administrator and log on to the page, the Administrator account creates the FSO. This is very important because a certain account has certain permissions, and the FSO requires some permissions to perform the function fully.

An Internet account (Iuser_machinename,machinename is the name of the server) generally has Read permissions, which means that users will not be able to write to the guest book file. However, there are several options to circumvent this problem.

First, it is also very difficult to ask the user to log in to the server before filling in the guest book. However, the main point of the guest book is to collect information from anonymous users, and if users are required to log in, they must know who they are. So skip this selection and see the next one.

The 2nd method is to create a directory or file that the Iuser_machinename user has write permissions for. Doing so may open up some potential security vulnerabilities because anyone who knows the correct directory and has some Web skills can populate the server with content. This is a very serious taboo. So you have to make sure that you keep the writable directory information in a hidden location, and that you can set these directories outside of the web directory structure (for example, under Windows, a directory that is not in the Inetpub directory).

You might think: OK, now I know how to write a file. But can you do more? Here's a try to create a search function for your Web site.

The key to building a search engine is recursion. Basically, write a section of code to search the files in the directory, and then execute the same code for all the directory loops. Because you cannot determine how many subdirectories are in total, you must execute the search code over and over until the end. Recursive call very good!

Below to create a search page. Suppose you've created an HTML form in which the user enters a search string.

Dim objfolder
Dim Strsearchtext
Dim objFSO

Strsearchtext = Request.Form ("SearchText")--The search string



Related Article

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.