IIS Development

IisDev

>  Home
>  News
>  Notification Service

 

Articles

>  Code Samples
>  ASP.NET
>  Security

 

Components & Tools

>  Free Components
>  SharpDevelop
>  ComponentCenter
>  CodeLook Program

 

Service

>  Links
>  Advertising
>  Contact us

Search IISDEV:

FileCache Component - free

Last updated: 11/19/99 (2.1), Faster, Bug fixes, Caching
Version 2.0: 08/15/99, Redesign, Absolute Path Feature added
Version 1.1: 12/11/98, Save As functionality added


How many times did you need to write content from a file to the client? Scenarios include common files that don't work with the SSI mechanism, or that you simply want to hide for example image files. Though you could use the FileSystemObject, this gets tedious. That's why we have created a component that takes care of everything: reading a file (including mapping the path), buffering it in memory, setting the MIME type and finally writing it to the client.

Version 2.0 added to this by allowing you to send any file that is located anywhere on your harddisk, no longer requiring that it is located in any of the directories of the Website. In 2.1, we have made it faster and fixed small bugs.

Before diving into three examples, we'd like to point out that the component uses the "old" OnStartPage and OnEndPage mechanism for acquiring the intrinsic objects (was done for IIS3 compatibility). If there's a need for the MTS compliant implementation (GetObjectContext calls), drop us an email.

How it Works in Your Code

The component does not support much functionality, however, you can read from a file, set the MIME type and write it to the client (sendimage.asp sample):

<%
  Set fcObj = Server.CreateObject("Softwing.FileCache.1")
  bResult = fcObj.InitFromFile("image.png", False)
  fcObj.MimeType = "image/png"
  bResult = fcObj.Write()
  Response.End
%>

Notice that the component sets the MIME type before writing to the client - this means that if you want to include other content than only from the file itself, you have to buffer the output (textmerge.asp sample):

<%
  Response.Buffer = True
  
  Set fcObj = Server.CreateObject("Softwing.FileCache.1")
  ' map it here, and therefore pass the component an absolute path
  strFile = Server.MapPath("sendzip.asp")
  bResult = fcObj.InitFromFile(strFile, True)

  fcObj.MimeType = "text/html"
  bResult = fcObj.Write()
  ' note the ASP code written above doesn't show up in IE
%>
<P>
To view the file that was merged, select View Source from
the context menu (ASP code was merged).

Now that's truly simple - just never forget that the MIME type is a header, which can be written only before any content is sent to the client (but I bet you already knew that!).

The Content-Disposition header feature allows you to send the "real" filename to the browser, for example: your script is called using xyz.asp?file=5&user=10 and the file you are about to transfer is named FileCache.zip, then the File Save As dialog will now show the latter (sendzip.asp sample):

<%
 Dim fcObj	' the FileCache instance
 Dim bResult	' result returned from component
 Const fFile2Send = "FileCache.zip"

 ' create the object
 Set fcObj = Server.CreateObject("Softwing.FileCache.1")

 ' Read from the file; additional boolean parameter
 ' for absolute (True)/relative (False) path
 bResult = fcObj.InitFromFile(fFile2Send, False) ' map the path

 If Not bResult Then
  Response.Write "The file could not be opened!"
  Response.End
 End If

 fcObj.MimeType = "application/zip"
 bResult = fcObj.Write()
 If Not bResult Then
  Response.Write "The contents of the file could not " & _
  	"be sent to the client!"
 End If
 Response.End
%>

Error Codes

The following errors can be returned from FileCache:

CFC_SAFE_ARRAY_ALLOC
vbObjectError + 1
This error can be returned by InitFromFile when not enough memory could be allocated to cache the file.
CFC_SMAPPATH_FAILED
vbObjectError + 2
Mapping a path fails (you have set the AbsolutePath parameter of InitFromFile to False) when it is invalid.
CFC_CREATEFILE_FAILED
vbObjectError + 3
Raised by InitFromFile when the file you passed in could not be opened for read access (either didn't exist or was locked)
CFC_ONSTARTPAGE
vbObjectError + 4
The components OnStartPage event was not called. This could happen if the component is run in Transaction Server or COM+ applications. FileCache cannot be used in these environments.
CFC_MAP_VIEW_OF_FILE
vbObjectError + 5
The input file could not be mapped into memory. The most common cause is that you are not running Windows NT 4.x upwards.

Download and Installation

Before using the component you have to register it using the regsvr32 utility:
   regsvr32 FileCache.dll.

Download (size is approx. 27KB)
The most current version number of the component is 2.1. It is compiled with Visual C++ 6.0 SP3 using ATL. It does not work on platforms other than NT 4.x or higher.

 

©1998-2004 Christoph Wille. All rights reserved.
All content and images on this site are copyright. Reuse (even parts) needs our written consent.