120 likes | 234 Views
This chapter outlines essential techniques for managing files in your e-commerce application, focusing on how to use the `#Include` file directive in ASP pages, redirect customers to new pages, and store customer information using File Access Components. It explains the benefits of including files for shared content and procedures, the use of the Redirect method versus Response.End, and the functionality of FileSystemObject and TextStream for creating, reading, and writing to text files. Discover key operations like copying, moving, and deleting files effectively.
E N D
Chapter 4 Working with Files in Your E-Commerce Application Objectives: • Working with Files in E-Commerce Application. • How to use #Include file in ASP page. • Redirect a customer to a new ASP page file. • Use File Access Component to store customer information in a text file.
Including files in an ASP Page • Can be included using server-side #INCLUDE directive (used with ASP & HTML files). • Reason to include files: • To add same content to a number of pages at your website. • When you need to use a standard set of functions and procedures within multiple ASP pages. • Two forms of the directive: • <!-- #INCLUDE FILE=“somefile.ASP” --> • <!-- #INCLUDE VIRTUAL=“somefile.ASP” --> • The files can be included dynamically.
Using File Redirection • The Request object has a method called Redirect to automatically redirect a customer to new file. • You can pass query string variables with redirect method but not TARGET attribute (redirecting to particular window/frame). • Not all browser support this.
Don’t Use Redirect Method • Reasons: • Place more strain on your Web server bcoz it forces the browser to retrieve two pages instead of one. • Redirects aren’t fully supported by all browsers. Will get the message like:302: Object has moved. • Instead use Response.End
Using the File Access Components • File Access Components are used to create and read text files. • Used to record information from HTML forms of Web Site.
Objects of File Access Components • FileSystemObject – Includes all the basic methods for working with the file system. • TextStream – Used for reading & writing to a text file. • File – Represents an individual file. • Folder– Represent a file folder (a directory). • Drive- Represents a disk drive or network share.
TextStream Object • CreateTextFile() method of FileSytemObject object is used to create text file and return reference to the TextStream Object. • It has 3 parameters of which one is compulsory and two are optional. They are: • FileSpecifier – Required parameter that specifies the path of the file to create. • Overwrite – Optional, by default value is TRUE. • Unicode-Optional, By default is FALSE. Indicates file using the ASCII character set should be created.
Three Methods of TextStream used to write a text file: • Write(string)- Writes a string to text file. • WriteLine(String)- Writes a string to text file and appends a newline character. String argument is optional. • WriteBlankLines(lines) – Writes indicated number of blank lines to the text file.
Reading a Text File • The four properties of FileSystemObjext used while reading are: • AtEndOfLine-New line character is detected. Default value is TRUE. • AtEndOfStream-Indicates the end of the text file is reached. • Column-This property indicates the current character position in a line. • Line-Indicates a current Line in a file.
Contd….. • The four objects of TextStream object used to read a text file are: • Read(character)- Reads specified number of characters from the text file. • ReadLine-Reads single line from the text file. • ReadAll()- Reads entire content of the text file. • Skip(character)- Skips specified number of characters. • SkipLine- Skips one line in text file.
Opening the Text File • The file must be opened before read from or write into it. • The following are the arguments used with OpenTextFile() method. • FileSpecifier-Required, specifies the path of the file to be open. • IOMode-Optional. Indicate file to be opened is to read/write/append. (1-R, 2-W,8-A). • Format-Optional. Specifies format of the file. By default it is ASCII.
Managing Text Files • Methods of FileSystemObject to manage file are: • CopyFile() • MoveFile() • DeleteFile() • FileExists() • Display the files in the folder use FileSystemObject & Folder object.