Chapter 9: Miscellaneous Features

Contents

9.1 AspUpload's Non-Upload Functionality

AspUpload's functionality is not limited to file uploading. It is a complete file management solution. Other AspUpload features include secure file downloading, directory listing, and ActiveX registration.

9.2 Secure File Downloading

With AspUpload, you can let your users download files which are not necessarily residing in a virtual directory, but anywhere on the server's hard drive or even a remote machine.

File downloading is implemented with the method Upload.SendBinary, as follows:

Set Upload = Server.CreateObject("Persits.Upload")
Upload.SendBinary "c:\path\file.ext", True, "application/octet-stream", True

The SendBinary method takes a physical file path as the first parameter. If the second parameter is set to True, SendBinary automatically builds the headers Content-Type, Content-Disposition and Content-Length. The third parameter is optional and specifies the Content-Type value. If this parameter is omitted, the method obtains Content-Type from the system registry based on the file extension.

The fourth optional parameter, if set to True, specifies whether the Content-Disposition header should contain the keyword "attachment;" This is necessary to force a "Save As" dialog box instead of opening the file in-place by the IE browser.

To use the SendBinary method, you need to provide a link on your page pointing to an ASP script which calls SendBinary. The sample files text.asp and text_download.asp demonstrate this technique:

text.asp:

<HTML>
<BODY>
<h3>File Downloads</h3>

Click on this link to download a file:<p>

<A HREF="text_download.asp">test.txt</A>

</BODY>
</HTML>

text_download.asp:

<%
' AspUpload Code samples: text_download.asp
' Invoked by text.asp
' Copyright (c) 2001 Persits Software, Inc

' This file must not contain any HTML tags

Set Upload = Server.CreateObject("Persits.Upload")

' Build path to file
Path = Server.MapPath("test.txt")

' Parmeters:
' 1. Path to file to download
' 2. Yes, build content-xxx headers
' 3. Use this value for Content-Type header
' 4. Include the word "attachment;" to Content-Disposition to force download

Upload.SendBinary Path, True, "application/octet-binary", True
%>

Click the link below to run this code sample:

9.3 Directory Listing

Besides Form and Files collections, UploadManager provides one more collection, Upload.Directory, which represents all files and subdirectories of a directory on your hard drive.

The Directory collection consists of DirectoryItem objects. Each DirectoryItem object represents a file or subdirectory inside this directory. All the file and subdirectory items are always grouped together, with subdirectories preceding files in the collection. Within the subdirectory and file groups, items can be sorted by name, type, size, creation time, last modification time and last access time.

The following code snippet creates and scrolls through a Directory collection which represents all files in the folder "c:\mydir" sorted by file type:

<%

<!--METADATA TYPE="TypeLib" UUID="{B4E1B2DE-151B-11D2-926A-006008123235}"-->

Set Upload = Server.CreateObject("Persits.Upload")
Set Dir = Upload.Directory( "c:\mydir\*.*", SORTBY_TYPE)
For Each item in Dir
   Response.Write item.FileName &"<BR>"
Next
%>

The first argument of the Directory property is a directory name and a file name which can contain wildcard characters (* and ?).

The second argument is optional and, if used, must be set to one of the Sort-by values defined in the AspUpload type library. The default value is SORTBY_NAME (numeric 1). The other valid values for the 2nd parameter are SORTBY_TYPE (2), SORTBY_SIZE (3), SORTBY_CREATIONTIME (4), SORTBY_LASTWRITETIME (5), and SORTBY_LASTACCESSTIME (6).

To use this and other constants defined in the AspUpload type library, use the metadata tag

<!--METADATA TYPE="TypeLib" UUID="{B4E1B2DE-151B-11D2-926A-006008123235}"-->

The third argument is also optional. It is a Boolean value which specifies whether to sort in an ascending (if set to True or omitted) or descending (if False) order.

The code sample DirectoryListing.asp in conjunction with the script download.asp demonstrates the directory listing and download functionality of AspUpload.

DirectoryListing.asp:

<HTML>
<HEAD>
<!--METADATA TYPE="TypeLib" UUID="{B4E1B2DE-151B-11D2-926A-006008123235}"-->
</HEAD>
<BODY>

<H3>Directory Listing</H3>

<%
If Request("Dir") = "" Then
   Directory = "c:\"
Else
   Directory = Request("Dir")
End If

Set Upload = Server.CreateObject("Persits.Upload")
Set Dir = Upload.Directory( Directory & "*.*", , True)
%>

<h2><% = Dir.Path %></h2>

<TABLE BORDER=1 CELLSPACING=0>
<TH>Name</TH><TH>Size</TH> <TH>Type</TH> <TH>Modified</TH> <TH>Created</TH><TH>Attr</TH><TR>
<% For Each Item in Dir %>

<% If Item.IsSubdirectory Then %>
   <TD><B><A HREF="DirectoryListing.asp?Dir=<% = Server.URLEncode(Left(Dir.Path, Len(Dir.Path)-3)) & Server.URLEncode(Item.FileName) & "\" %>"><% = Item.FileName %></A></B></TD>
   <TD><B>DIR</B></TD>
<% Else %>
   <TD><A HREF="Download.asp?Name=<% =Server.URLEncode( Item.FileName )%>&File=<% = Server.URLEncode(Left(Dir.Path, Len(Dir.Path)-3)) %><% =Server.URLEncode( Item.FileName )%>"><% = Item.FileName %></A></TD>
   <TD ALIGN=RIGHT><% = Item.Size %></TD>
<% End If %>

<TD><% = Item.FileType %></TD>
<TD><% = Item.LastWriteTime %></TD>
<TD><% = Item.CreationTime %></TD>
<TD> 
<%
If Item.CheckAttribute( FILE_ATTR_READONLY) Then
   Response.Write "R"
End If
If Item.CheckAttribute( FILE_ATTR_HIDDEN) Then
   Response.Write "H"
End If
If Item.CheckAttribute( FILE_ATTR_SYSTEM) Then
   Response.Write "S"
End If
If Item.CheckAttribute( FILE_ATTR_ARCHIVE) Then
   Response.Write "A"
End If
%>
</TD><TR>
<% Next %>
</TABLE>
</BODY>
</HTML>

download.asp

<%
' AspUpload Code samples: download.asp
' Invoked by DirectoryListing.asp
' Copyright (c) 2001 Persits Software, Inc
' http://www.persits.com

' This file must not contain any HTML tags

Set Upload = Server.CreateObject("Persits.Upload")
Upload.SendBinary Request("File"), True, "application/octet-stream", True
%>

Click the link below to run this code sample:

9.4 Interactive File Deletion

The Directory Listing functionality can be used to provide an interface for interactive file deletion from the server. The code sample DeleteFiles.asp lists files in an arbitrary directory with checkboxes next to each file. A user may check any or all boxes and click the Delete button. Note that the line Upload.DeleteFile is commented out for security purposes.

This code sample also demonstrates sorting by various columns.

<HTML>
<BODY>
<H3>File Deletion</H3>
<%
   Directory = "c:\" ' initial directory
   Set Upload = Server.CreateObject("Persits.Upload.1")
   Set Dir = Upload.Directory( Directory & "*.*", Request("sortby"))

   ' perform deletions if this is a form submission
   If Request("Delete") <> "" Then
      For Each Item in Request("FileName")
         Response.Write "Deleting File " & Item & "</B><BR>"
      ' uncomment next line to enable deletions.
      ' Upload.DeleteFile Directory & Item
      Next
   End If
%>

<h3><% = Dir.Path %></h3>

<FORM ACTION="DeleteFiles.asp" METHOD="POST">
<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="0" STYLE="font-size: 8pt;">
<TR><TH>&nbsp;</TH>
<TH><A HREF="DeleteFiles.asp?sortby=1">Name</A></TH>
<TH><A HREF="DeleteFiles.asp?sortby=3">Size</A></TH>
<TH><A HREF="DeleteFiles.asp?sortby=2">Type</A></TH>
<TH><A HREF="DeleteFiles.asp?sortby=5">Date</A></TH></TR>
<% For Each File in Dir %>
<% If Not File.IsSubdirectory Then %>
<TR>
   <TD><INPUT TYPE="CHECKBOX" VALUE="<% = Server.HTMLEncode(File.FileName)%>" NAME="FileName"></TD>
   <TD><% = File.FileName %></TD>
   <TD><% = File.Size %></TD>
   <TD><% = File.FileType %></TD>
   <TD><% = File.LastWriteTime %></TD>
</TR>
<% End If
Next
%>
<TR>
<TD COLSPAN="5">
<INPUT TYPE="HIDDEN" NAME="sortby" VALUE="<% = Request("sortby") %>">
<INPUT TYPE="SUBMIT" NAME="Delete" VALUE="Delete selected files">
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

Click the link below to run this code sample:

9.5 ActiveX Registration

UploadManager provides the method RegisterServer that mimics the behavior of the REGSVR32 utility. You can use it to automatically register ActiveX DLLs being uploaded:

<%
Set Upload = Server.CreateObject("Persits.Upload")
Upload.Save "c:\Upload"
For Each File in Upload.Files
   Upload.RegisterServer File.Path
Next
%>

If the optional second parameter is set to False, the method will unregister the library:

...
Upload.RegisterServer File.Path, False
...

9.6 Encryption Support

AspUpload can be used in conjunction with another Persits Software component, AspEncrypt to encrypt files as they are being uploaded, and decrypt them as they are being downloaded.

File encryption is beyond the scope of this manual. For more information on AspEncrypt-aided secure file uploading and downloading, see the chapter Implement Secure File Uploading and Downloading of the AspEncrypt manual.

9.7 Introducing XUpload and JUpload

In traditional file uploading, files are selected with an HTML form via <INPUT TYPE="FILE"> controls. This method has many limitations:

  • Only one file can be selected at a time;
  • There is no way to select an entire folder;
  • Original date information cannot be preserved on an uploaded file;
  • The Select File dialog cannot be customized (e.g. it cannot be configured to display images only);
  • The initial directory for the Select File dialog cannot be specified;
  • File size and type restrictions cannot be enforced before an upload begins.

To overcome these limitations, we offer two client-side upload agents that replace the traditional HTML enctype="multipart/form-data" form, and offer a simple and user-friendly interface for selecting files and even entire folders.

XUpload is an ActiveX control which looks and acts like a standard Windows list control. Its only limitation that is runs under IE in a Windows environment only.

JUpload is a Java applet which is even more feature-rich than XUpload, but best of all is runs equally well under the IE and Netscape browsers.

Update (November 2023): Since no major browser supports ActiveX controls or Java applets anymore, the XUpload and JUpload products are no longer available.