File Object

File Transfer CS File objects represent system files that you can access with your Active Server Pages application. File objects can represent files uploaded by the Post object, files listed by the Directory object, or specific files that you wish to manipulate directly. These files can be located anywhere on your system including paths that are not web shared.

Collections ACEs

Properties  Attributes, FileExtension, FileName, FileType, LastModified, MD5, MimeType, Name (default property), Owner, Size

Methods  Compare, Copy, Create, CreateTemp, Delete, Download, Exists, Export, Import, IsDirectory, IsReadonly, Refresh, RegisterDll, Rename

 

 

  Collections

ACEs

Collection of ACE objects for the file (NTFS only).
Access: Read/Write
Default: None
Notes: The ACEs collection contains all of the Access Control Entries (ACE) listed in a file's Access Control List (ACL).  The index into this collection is the ACE trustname - the user or group the ACE is associated with.  For more information see the ACEs collection.

Example 1: The following example shows how to display each user or group with having an ACE setting for a file.

' Code showing initialization of the File object omitted
For Each ACE In File.ACEs
	Response.Write "Name = " & ACE.TrusteeName & "<BR>"
Next

Example 2: The following example shows how to give a user read and execute permission for a file.

' Code showing initialization of the File object omitted
File.ACEs.Add "Sam",AF_PERMIT_READ And AF_PERMIT_EXECUTE, AF_INHERIT_NONE

See Also: ACE object, ACEs collection

 

 

  Properties

Attributes

A long containing a bit mask that represents the file attributes of the file.
Access: Read/Write
Default: None
Notes:  This property contains various file system attributes for the file represented as a bit mask. Boolean operations can be used to test and set specific attributes using the constants defined in the table below.

Constant Value Description
AF_ATTRIBUTE_READONLY 1 The file is read only.
AF_ATTRIBUTE_HIDDEN 2 The file is a hidden file.
AF_ATTRIBUTE_SYSTEM 4 The file is a system file.
AF_ATTRIBUTE_DIRECTORY 16 The file is a directory (Read only)
AF_ATTRIBUTE_ARCHIVE 32 The file has the archive attribute set.
AF_ATTRIBUTE_COMPRESSED 2048 The file is compressed (NTFS only and Read only).

The attributes AF_ATTRIBUTE_DIRECTORY and AF_ATTRIBUTE_COMPRESSED can not be modified.

Example 1: The following example shows how to test to see if the file is hidden:

' Code showing initialization of the File object omitted
If File.Attributes And AF_ATTRIBUTE_HIDDEN Then
    Response.Write "File is hidden.<BR>" 
Else
    Response.Write "File is not hidden.<BR>"
End If

Example 2: The following example shows how to set the read only attribute for a file:

' Code showing initialization of the File object omitted
File.Attributes = File.Attributes Or AF_ATTRIBUTE_READONLY

See Also: Directory object

FileExtension

String value of the file extension (if any) of the file.

Access: Read
Default: None

Notes: The FileExtension property represents the file extension portion of the file name that is automatically derived from the Name property. The file extension includes those characters to the right of the last period ('.') appearing in the file name.

Example: The following example displays the file extension of a file:

' Code showing initialization of the File object omitted
Response.Write "File extension = " & File.FileExtension & "<BR>"

See Also: File.FileName property, File.Name property

FileName

String value of the name (excluding path) of the file.

Access: Read
Default: None

Notes: The FileName property represents the file name of the file being referenced by the File object. This property is automatically derived from the Name property. Unlike the Name property, FileName does not include the path information.

Example: The following example displays the file name of a file:

' Code showing initialization of the File object omitted
Response.Write "File name = " & File.FileName & "<BR>"

See Also: File.FileExtension property, File.Name property

FileType

String value of the type description of the file.

Access: Read
Default: None

Notes: The FileType property represents the type description of the file currently being referenced by the File object. This value is determined by using the file extension to looking up the related file type information stored in the Windows registry.

Example: The following example displays the file type of a file:

' Code showing initialization of the File object omitted
Response.Write "File type = " & File.FileType & "<BR>"

See Also: File.FileExtension property

LastModified

Date value of the time the file was last modified.

Access: Read/Write
Default: None

Notes: The LastModified property represents the date and time that the file currently being referenced by the File object was last modified.

Example: The following example displays the last modified date of a file:

' Code showing initialization of the File object omitted
Response.Write "Last modified = " & File.LastModified & "<BR>"

MimeType

String value of the mime type of the file based on the Windows registry.

Access: Read
Default: application/unknown

Notes: The MimeType property represents the MIME type of the file currently being referenced by the File object. This value is determined by looking up the related file type information stored in the Windows registry. This is the same MIME type information used by IIS for sending HTTP responses. If a MIME type value can not be determined for a given file type, the property is set to "application/unknown". The MimeType property is used by the Download method when no ContentType parameter is specified.

Example: The following example displays the MIME type of a file:

' Code showing initialization of the File object omitted
Response.Write "MIME type = " & File.MimeType & "<BR>"

See Also: File.FileType property

MD5

String value of the message digest.

Access: Read
Default: None

This property returns a message digest as derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.  The message digest is a checksum of the contents of the file.  The message digest is returned as a hexidecimal string value whose length is 32 characters.

Example: The following example displays the message digest of a file:

' Code showing initialization of the File object omitted
Response.Write "MD5 = " & File.MD5 & "<BR>"

Name

String value of the name (including path) of the file.

Access: Read/Write
Default: None

Notes: Name is the default property of the File object representing the path of the file being referenced. Because this is the default property the following are equivalent:

Name = File.Name
Name = File

This property is automatically set by the Create and CreateTemp methods to the path of the file that was just created.

While you can set the Name property to point to an existing file, modifying the Name property will not cause the file to be renamed. If you want to rename a file, use the Rename method.

Example: The following example displays the full name of a file:

' Code showing initialization of the File object omitted
Response.Write "Name = " & File.Name & "<BR>"

See Also: File.FileExtension property, File.FileName property

Owner

String value representing the owner of the file (NTFS only). This may be a group or a user.

Access: Read
Default: None

Notes:  This property can be useful for displaying the owner of a file.  While it can also be used to take ownership of file, you must be logged in under the same account that you are trying to set this property to and that account must have permission to take ownership of the file. For more information see Security Considerations.

Example: The following example displays the owner of a file:

' Code showing initialization of the File object omitted
Response.Write "File owner = " & File.Owner & "<BR>"

Example 2: The following example shows how to take ownership of a file:

' Code showing initialization of the File object omitted
File.Owner = "sam"

See Also: Security Considerations

Size

Long integer representing the size in bytes of the file.

Access: Read
Default: None

Notes: The Size property represents the length of the file in bytes. If your application uses another control to update the file, or an external process may have updated the file, call the Refresh method to update the Size property.

Example: The following example displays the size of a file:

' Code showing initialization of the File object omitted
Response.Write "File size = " & File.Size & "<BR>"

See Also: File.Refresh method

 

 

 

  Methods

Compare

Compare Name

Performs a binary comparison of the file object with the one specified by the Name parameter and returns True if the files are the same.

Parameter Description
Name String containing the path for the file to be compared.

Returns: A boolean indicating if the files are identical

Notes: The Compare method is useful for determining if two files are identical.

Example: The following example compares file1.txt and file2.txt:

Set File = Server.CreateObject("ActiveFile.File")
File.Name = "C:\file1.txt"
If File.Compare("C:\file2.txt") Then
    Response.Write "Files are identical<BR>"
Else
    Response.Write "Files do not match<BR>"
End If

See Also: File.Name property

Copy

Copy Name

Copies the file to the target file specified by Name.

Parameter Description
Name String containing the Target path for the Copy operation.

Returns: A File object referencing the target file.

Notes: The Copy method allows you to make a copy of the file currently being referenced by the File object. The return value can be used to access the new file while the old File object still references the original file.

! If the target file specified by Name parameter already exists, it is overwritten.

Example: The following example copies file1.zip and file2.zip:

Set File1 = Server.CreateObject("ActiveFile.File")
File1.Name = "C:\file1.zip"
Set File2 = File1.Copy("C:\file2.txt")

Create

Create Name

Creates a new file.

Parameter Description
Name String containing the path for the new file.

Returns: Nothing
Notes: The Create method allows you to create a zero length file specified by the Name parameter.

! If the file specified by Name parameter already exists, it is overwritten with an empty file.

Example: The following example shows how to create a new file:

Set File = Server.CreateObject("ActiveFile.File")
File.Create "C:\myfile.doc"

See Also: File.CreateTemp method

CreateTemp

CreateTemp Path

Creates a temporary file in the directory/folder specified by Path.

Parameter Description
Path String containing the directory or folder path that will contain the temporary file.

Returns: Nothing

Notes: The CreateTemp method creates a temporary file with a unique name in the directory specified by the Path parameter. The directory must already exist.

Example: The following example shows how to create a temporary file:

Set File = Server.CreateObject("ActiveFile.File")
File.CreateTemp "C:\temp"

See Also: File.Create method

Delete

Deletes the file associated with the File object.

Returns: Nothing
Notes: The Delete method allows you to permanently remove a file from the file system. If the File object is referencing a directory, the directory will be deleted only if it is empty. See the Directory.Delete method for deleting non-empty directories.

! There is no undelete; once a file is deleted using this method, it can not be restored.

Example: The following example shows how to delete a file:

' Code showing initialization of the file object omitted
File.Delete

See Also: Directory.Delete method

Download

Download {Mimetype},{ModDate},{ResponseEnd},{DeleteFile}

Downloads a file to the client.

Parameter Description
Mimetype Optional string overriding the current Mimetype of the file.
ModDate Optional date specifying what date should be specified in the HTTP Last-Modified header.  Defaults to the LastModified property.
ResponseEnd Optional boolean to automatically end the response once the file has been downloaded.  If you are experiencing script timeout problems try using this flag.
DeleteFile Optional boolean to delete the file being downloaded after successful download.

Returns: True if downloading the file was successful.  Any error, or if the client disconnects while downloading the file will return false.

Notes: The File.Download method allows you to send the file referenced by the File object as an HTTP response. This method is useful for controlling how files from the server are sent to clients. The MimeType parameter specifies the value to use to identify the content type of the file for the HTTP response. If the Mimetype parameter is omitted, the MimeType property is used.

! If your Active Server Page contains any HTML tags or Response.Write calls, steps need to be taken to ensure that only the contents of the file are sent to the client. One way to eliminate extraneous HTML tags from your response is to follow these steps:
  1. Set Response.Buffer = True at the beginning of your page
  2. Call Response.Clear prior to calling File.Download
  3. Call Response.End as soon as you have finished processing the file or set the ResponseEnd parameter to True.

In addition to sending the contents of the file to the client, File.Download also sets the HTTP Last-Modified header to indicate when the file was last modified. This allows both browser client caching and proxy server caching to be supported by recognizing and responding appropriately to HTTP requests that contain the If-Modified-Since header. Depending on your application, you can use the ModDate parameter to control whether or not cached versions of the downloaded file are used as follows.

  • If you are downloading a permanent file, caching will automatically be based on the LastModified property of the file. In this case you do not need to specify a ModDate parameter if you want caching to work.
  • If you are downloading a temporary file that was created on the fly, you will need to set the ModDate parameter to an appropriate value if you want caching to work. For instance, if you are downloading a file exported from a database, you may want to set the ModDate parameter to a date stored in the database record that represents when the database record was last updated.
  • To effectively disable caching, simply specify Now() as the ModDate parameter

An added bonus to supporting caching in your application is support for restarting downloads. In the past, if a client's network connection was lost during the download of a large file, the client would have to start the download all over again. Today's browsers are now capable of picking up right where they left off. To support this, File.Download will send only the portion of the file that was not yet downloaded instead of sending the entire file.

Example: The following example shows how to download a file to a browser:

' Code showing initialization of File object omitted
File.Download 

See Also: Downloading a file from the server

Exists

Exists

Returns true if the file exists.

Returns: A boolean indicating whether the file exists or not.

Notes:  The Exists method is useful for checking for the existence of a file.

Example: The following example shows how to test to see if a file exists:

' Code showing initialization of the File object omitted
If File.Exists Then
    Response.Write "File exists.<BR>"
Else
    Response.Write "File does not exist.<BR>"
End If

See Also: Directory.Exists method

Export

Export adoFieldObj, Append

Exports the Active Data Object (ADO) Field Object into the file.

Parameter Description
adoFieldObj An ADO field object of type long binary.
Append Boolean flag indicating whether data should be appended to an existing file.

Returns: Nothing

Notes: The Export method allows you to export files stored in databases that support ADO and BLOB data types. The adoFieldObj parameter must point to column of type adLongVarBinary of an existing row of an ADO RecordSet. After calling Export, the file pointed to by the File object will contain the data from the database.

! If the File object points to a file that already exists and the Append parameter is omitted or specified as False, Export will overwrite the file contents.

Example: The following example shows how to export a file from a database:

' Code showing initialization of File object and ADO RecordSet object omitted
File.Export Rs.Fields("BLOB")

See Also: Importing and exporting files from a database

Import

Import adoFieldObj

Imports the file into an Active Data Object (ADO) Field Object.

Parameter Description
adoFieldObj An ADO field object of type long binary.

Returns: Nothing

Notes: The Import method allows you to store files in databases that support ADO and BLOB data types. The adoFieldObj parameter must point to a column of type adLongVarBinary of an existing row of an updateable ADO RecordSet. After calling Import, the Update method of the ADO RecordSet object must be called to complete the update.

Example: The following example shows how to import a file into a database:

' Code showing initialization of File object and ADO RecordSet object omitted
File.Import Rs.Fields("BLOB")

See Also: Importing and exporting files from a database

IsDirectory

IsDirectory

Returns true if the file is a directory.

Returns: A boolean indicating whether the file is a directory.

Notes:  This feature is provided for compatibility with version 1.x of ActiveFile.  Use the Attributes property for future compatibility.

IsReadonly

IsReadonly

Returns true if the file is read-only.

Returns: A boolean indicating whether the file is read-only or not.

Notes:  This feature is provided for compatibility with version 1.x of ActiveFile.  Use the Attributes property for future compatibility.

Refresh

Refresh

Refreshes the attributes of a file object.

Returns: Nothing
Notes:  Refresh should be used if the underlying file has been changed by another component or external process.

Example: The following example shows how to refresh the File object:

' Code showing initialization of the File object omitted
File.Refresh

RegisterDll

RegisterDll

Registers the DLL pointed to by the File object in the registry.

Returns: Nothing

Notes:  RegisterDll will install self-registering DLLs in the registry.  The process must have write access to the registry.  For more information see Security Considerations.

Example: The following example shows how to register a DLL:

Set File = Server.CreateObject("ActiveFile.File")
File.Name = "C:\WINNT\system32\mycomp.dll"
File.RegisterDll

See Also: File.Name property

Rename

Rename Name

Renames the file to the target file specified by Name.

Parameter Description
Name String containing the Target path for the Rename operation.

Returns: Nothing
Notes: The Rename method allows you to rename or move the file currently being referenced by the File object. Afterwards, the File object references the file in the new location.

Example: The following example shows how to rename file "old.zip" to "new.zip":

Set File = Server.CreateObject("ActiveFile.File")
File.Name = "C:\old"
File.Rename "C:\new"

See Also: File.Name property