Fath Upload for ASP

FathUpload is a COM component for ASP applications, enabling them to accept easily and reliably huge uploads to the web server with a browser. The files are selected for uploading via an HTML POST form using the <INPUT TYPE=FILE> tag.

Main features:

  • Low resource usage
  • Can handle huge uploads (up to 2Gb)
  • Speed
  • Reliability
  • Robustness
  • HTML upload progress bar
  • Unicode support
  • Ability to upload multiple files at once
  • Access to text items on the form
  • Uploads to memory
  • Ability to save files in the database
  • Automatic generation of unique file names to prevent collisions with existing files
  • Ability to put a limit on the size of files being uploaded
  • Ability to change file attributes
 
 
 
Download Buy

Fath.Upload object reference

Properties:

File Represents uploaded file
FileCount Returns number of files accessible with File property
Field Represents field on a upload form
FieldCount Returns number of fields
MaxFileSize Sets maximal file size allowed
MaxUploadSize Sets maximal upload size allowed
OverwriteFiles Indicate does existing files can be overwriten
ProgressID Sets progress ID to use when reporting upload progress
TotalSize Returns total size of upload data
TransferAborted Returns TRUE if transfer was aborted by the client
CodePage Specify code page to use while translating data

Methods:

Receive Main method. Uploads, parses and saves files and fields
SendBinary Send a file to client for download
Clear Clears resources allocated by Fath.Upload
CreateProgressID Returns unique progress ID.
GetProgressInfo Retrieve progress info about upload.
FreeProgressInfo Frees progress info after upload is finished.
FileExist Checks if file exist.
LogonUser Impersonate another user
RevertToSelf Ends impresonation

 

IFile object reference

IFile object represents uploaded file.

Properties:

Attributes Read/Write Long Get/Set file-system attributes of saved file
ContentType Read only String Get Content-Type header value of uploaded file
Ext Read only String Get filename extension 
FileName Read only String Returns name of file without path
Folder Read only String Returns folder name where file is.
Name Read only String Returns form field name
OriginalFileName Read only String Client's original file name
OriginalFolder Read only String Client's original file folder
OriginalPath Read only String Client's original full path
Path Read only String Full path in local file-system of saved file
Size Read only Long Size of the file

Methods:

GetChunk Returns stream of file data.
Delete Deletes uploaded file from local server
SaveAs Saves uploaded file under different name or in different location

 

 


 

IField object reference

IField object represents a form field in user's upload data.

Properties:

Name Name of the field in form.
Value Textual value of the field.

 

 

 


 

File property (read-only)

Returns IFile object representing uploaded file. Receive method must be called before this property can be used.

Syntax

oUpload.File (Index As Variant) As IFile

Parameters

As Index parameter you may use a 1-based integer index, or a string corresponding to the NAME attribute of an <INPUT TYPE="FILE"> item of your upload form.

Returns

IFile object

Remarks

none

Example

Response.write "<b>Uploaded files</b><br>"

For i = 1 to oUpload.FileCount

    Response.write i & ") "& oUpload.File(i).Filename & " (" & oUpload.File(i).Size & ")<br>"

Next

 


FileCount property (read-only)

Returns number of uploaded files. Receive method must be called before this property can be used.

Syntax

oUpload.FileCount As Long

Returns

Long (number) value

Remarks

none

Example

Response.write "<b>Uploaded files</b><br>"

For i = 1 to oUpload.FileCount

    Response.write i & ") "& oUpload.File(i).Filename & " (" & oUpload.File(i).Size & ")<br>"

Next

 


Field property (read-only)

Returns IField object representing a field on a uploaded form. Receive method must be called before this property can be used.

Syntax

oUpload.Field (Index As Variant) As IField

Parameters

As Index parameter you may use a 1-based integer index, or a string corresponding to the NAME attribute attribute of a text item of your upload form.

Returns

IField object

Remarks

none

Example

Response.write "<b>Uploaded fields</b><br>"

For i = 1 to oUpload.FieldCount

    Response.write i & ") "& oUpload.Field(i).Name & " = " & oUpload.Field(i).Value & "<br>"

Next

 


FieldCount property (read-only)

Returns number of fields on a form. Receive method must be called before this property can be used.

Syntax

oUpload.FieldCount As Long

Returns

Long (number) value

Remarks

none

Example

Response.write "<b>Uploaded fields</b><br>"

For i = 1 to oUpload.FieldCount

    Response.write i & ") "& oUpload.Field(i).Name & " = " & oUpload.Field(i).Value & "<br>"

Next

 


MaxFileSize property 

Get/Set maximum allowed size of uploaded file.

Syntax

oUpload.MaxFileSize As Long

Returns

Long (number) value

Remarks

Use values in range from 0 to 2Gb.

Example

oUpload.MaxFileSize = 100 * 1024 ' equals 100Kb

oUpload.Receive "c:\upload"

 


MaxUploadSize property 

Get/Set maximum allowed size of upload.

Syntax

oUpload.MaxUploadSize As Long

Returns

Long (number) value

Remarks

Use values in range from 0 to 2Gb.

Example

oUpload.MaxUploadSize = 100 * 1024 ' equals 100Kb

oUpload.Receive "c:\upload"

 


CodePage property 

Enables translation of characters in POSTed text items and file names from the specified code page to Unicode. If the data is posted in the UTF-8 format, set this property to 65001. Other valid values include, but are not limited to, 1251 (Cyrillic), 1255 (Hebrew), 1256 (Arabic), etc.

Syntax

oUpload.CodePage As Long

Returns

Long (number) value

Remarks

Default is ASCII .

If you want to use Unicode characters in text data or the names of files you are uploading, you should instruct your browser to POST all the information in the UTF-8 format. This is done by including the following tag in the header of your page:

 

<HEAD>
...
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</HEAD>

 

On the FathUpload side, you must enable UTF-8 translation by setting the property CodePage to 65001 (a Win32-defined value for CP_UTF8)

 

sample upload.asp file:

<HTML>

<HEAD>

 <META http-equiv="Content-Type" content="text/html; charset=utf-8"> 

</HEAD> 

<BODY> 

<% Set Upload = Server.CreateObject("fath.Upload") 

' Enable UTF-8 translation 

Upload.CodePage = 65001 

Upload.Receive "c:\temp" 

%>

Files:<BR> 

<%

Response.Write Upload.File(1).Name & "= " & Server.HTMLEncode(Upload.File(1).Path) & " (" & Upload.File(1).Size &" bytes)<BR>" 

%>

</BODY> </HTML>

 

This script uses Server.HTMLEncode on file names and text items. This converts Unicode strings to a format understandable by a browser, such as &#1055;&#1077;&#1088;&#1089;&#1080;&#1094;.

Example

oUpload.CodePage = 65001 'UTF-8

oUpload.Receive "c:\upload"

 


OverwriteFiles property 

If set to False (default), FathUplaod generates a unique file name by appending a numeric index in parentheses to the original file name if a file with this name already exists in the upload directory.

Syntax

oUpload.OverwriteFiles As Boolean

Remarks

none


ProgressID property 

Useful with HTML uplaod progress bar. Assign value returned from CreateProgressID method to this property in your upload page and in your progress page.
See HTML progress bar topic for more info.

Syntax

oUpload.ProgressID As String

Remarks

none


TotalSize property 

Returns total size of client's upload data in bytes.

Syntax

oUpload.TotalSize As Long

Remarks

none


TransferAborted property (read only)

Returns TRUE if upload was canceled by the client pressing STOP button in his browser.

Syntax

oUpload.TransferAborted As Boolean

Remarks

Useful if you must perform some cleanup procedure after unsuccessful upload.

 


GetChunk method (read only)

Returns stream of data from uploaded file.

Syntax

oUpload.File(Index).GetChunk ( Position As Long, Size As Long)

Remarks

Returns stream (SafeArray) object of file data starting at Position of size Size. It can be used as an argument for ADODB.Field.AppendChunk method for storing files in database.

Sample

rs.AddNew
rs("image_blob").Value = oUpload.File(Index).GetChunk (0, oUpload.File(Index).Size)
rs.Update

 

 

 


Receive method 

The main "workhorse" method which captures uploaded files, saves them on disk or in memory, and populates the Files and Fields properties.
Path, if specified, is the destination directory where files are to be saved.

If Path is an empty string ( "" ), files are saved in memory. A file in memory can be accessed via the property File.GetChunk or saved to hard drive via the method File.SaveAs.

Syntax

oUpload.Receive ( Path As String)

Remarks

none

Sample

<%

Response.Buffer = true
Server.scripttimeout=1800
Set oUpload = Server.CreateObject("Fath.Upload")
oUpload.ProgressID=Request("PID")
oUpload.OverwriteFiles=True

oUpload.Receive("c:\temp")

Response.write "<b>Uploaded files and form fields</b><br>"

For i = 1 to oUpload.FileCount
    Response.write i & ") "& oUpload.File(i).Filename <br>"
Next

For i = 1 to oUpload.FieldCount
    Response.write i & ") "& oUpload.Field(i).Name <br>"
Next

TotalSize=oUpload.TotalSize
Response.write "<br>"
Response.write "Total size: " & TotalSize & "<br>"
Set oUpload=Nothing

%>

 

 


SendBinary method 

"Smart download" method. Sends a local file to the clients browser for download. The main advantage of using this method is low system resource usage on the server.

Syntax

oUpload.SendBinary ( Path As String , ContentType As String)

Remarks

Path parameter specifies the full path to the local file to send.
ContentType is a string to send as Content-Type header for download. If it is empty, Fath Upload uses "application/octet-stream" .

Sample

<%

Response.Buffer = true
Server.scripttimeout=1800
Set oUpload = Server.CreateObject("Fath.Upload")

oUpload.SendBinary ( "c:\files\4download.zip" , "" )

Set oUpload=Nothing

%>

 


Clear method 

Frees resources allocated by Fath.Upload component.

Syntax

oUpload.Clear

Remarks

Call this method on the end of your ASP script.


FileExist method 

Returns TRUE is file specified by Path parameter exists.

Syntax

oUpload.FileExist ( Path As String ) As Boolean

Remarks

none

 

 


LogonUser method 

Impersonates the specified user account. If Domain is empty, the local computer will be used to validate the password. Use this function if your current security context does not allow you to perform an upload to a remote machine or other operations.

Syntax

oUpload.LogonUser ( Domain As String , Username As String , Password As String)

Remarks

none


RevertToSelf method 

Ends an impersonation begun by LogonUser method.

Syntax

oUpload.RevertToSelf

Remarks

none


CreateProgressID method 

Returns unique string used for reporting / retrieving upload progress info .

Syntax

oUpload.CreateProgressID () As String

Remarks

See HTML progress bar topic for more info.

 

 


FreeProgressInfo method 

Frees shared memory resource allocated for upload progress information. Call this method in your progress.asp page when it is about to close .

Syntax

oUpload.FreeProgessInfo () 

Remarks

See HTML progress bar topic for more info.

 

 


GetProgressInfo method 

Retrieves upload progress information. Before using this method, set ProgressID property.

Syntax

oUpload.GetProgessInfo (
                ReceivedBytes As Long,
                TotalBytes As Long,
                Seconds As Long,
                Status As Long

Remarks

Seconds returns total seconds elapsed since the start of upload.
Status values are: 0 - starting, 1 - uploading, 3 - upload completed, 4 - aborted (or an error occured).

See HTML progress bar topic for more info.

 

 

 


 

HTML progress bar

Basically, to upload, you need three ASP pages; form.asp , upload.asp and progress.asp .

Form.asp is a HTML form which accepts clients files and text fields.
Upload.asp processes uploaded files and fields.
Progress.asp shows upload progress.
It's important to put "<%@EnableSessionState=False%>" line on top of your progress.asp page for progress bar to work.

Here're the working examples:

FORM.ASP

<%
Response.Buffer = true
Server.scripttimeout=1800
%>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</HEAD>
<script language="javascript">
function DoUpload() {
  var PID= (new Date()).getTime() % 1000000000;
  window.open("progress.asp?a=progress&PID=" + PID,PID,     "height=70,width=400,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no");
  document.UploadForm.action = "upload.asp?PID=" + PID;
  document.UploadForm.submit();
}
</script>
<%
Response.write "<form Action=upload.asp Method=Post name=UploadForm Enctype=multipart/form-data>"
Response.write "<b>Files to upload</b><br>"
For i = 1 to 4
    Response.write "<Input Type=File size=25 Name=File" & i & "><br>"
Next
Response.write "<Input Type=Submit Value=Upload OnClick='DoUpload()' >"
%>

 

UPLOAD.ASP

<%
Response.Buffer = true
Server.scripttimeout=1800
Set oUpload = Server.CreateObject("Fath.Upload")
oUpload.ProgressID=Request("PID")
oUpload.OverwriteFiles=True
StartTime=Now
oUpload.Receive("c:\temp")
Response.write "<b>Uploaded files</b><br>"
For i = 1 to oUpload.FileCount
  Response.write i & ") "& oUpload.File(i).Filename & " ("
                        & SizeString(oUpload.File(i).Size) & ")<br>"
Next
TotalSize=oUpload.TotalSize
TimeTaken=Datediff("s",starttime,Now)
If TimeTaken=0 Then TimeTaken=1
Response.write "<br>"
Response.write "Total size: " & SizeString(TotalSize) & "<br>"
Response.write "Time taken: " & TimeTaken & "s<br>"
Response.write "Speed: " & SizeString(Round(TotalSize/TimeTaken)) & "/s"
Set oUpload=Nothing
End If

'########################
Function SizeString(size)
'########################
If Size>1024*1024 Then
SizeString=Round(Size/1024/1024,1) & "MB"
Else
SizeString=Round(Size/1024) & "kB"
End If
End Function
%>

PROGRESS.ASP

<%@EnableSessionState=False%>

<font size=2 face=Tahoma, Arial, Helvetica>
<%
'### Init
PID=Request("PID")
Set oUpload = Server.CreateObject("Fath.Upload")
oUpload.ProgressID=PID
oUpload.GetProgressInfo ReceivedBytes, TotalBytes, ElapsedTime, UploadStatus
If UploadStatus>2 Then oUpload.FreeProgressInfo 'Reset upload instance on Error or cancellation

'### Calculations
If ElapsedTime>0 Then ' If started
Speed=SizeString(Round(ReceivedBytes/ElapsedTime)) & "/s"
Percent= Round((ReceivedBytes/TotalBytes)*100)
RemainingTime=((TotalBytes-ReceivedBytes)/ReceivedBytes)*ElapsedTime
End If

'### Refresh progress window
If RemainingTime>60 Then Refresh=3 Else If RemainingTime>10 Then Refresh=2 Else Refresh=1
If UploadStatus<3 Then Response.Addheader "Refresh", Refresh

'### Output to screen
If UploadStatus>2 Then 'Close window on completion or error
Response.write "<script language=javascript>" & VbCrLf
Response.write "top.close();" & VbCrLf
Response.write "</script>" & VbCrLf
End If
Set oUpload=Nothing
%>
<body bgcolor="#EEEEEE">
&nbsp;<b>Uploading files</b>
<table border=0 width="100%"><tr bgcolor="#BBBBBB"><td>
<table border=0 width="<%=Percent%>%" cellspacing=0 bgcolor="#000080"><tr><td align=center><font size=2 color=FFFFFF><%=Percent%>%</font></td></tr></table>
</td></tr></table>
<font size=1>
<%
If UploadStatus>0 Then
Response.write "&nbsp;Remaining time: " & SecondsToTime(RemainingTime) & " (" & SecondsToTime(ElapsedTime) & " of " & SecondsToTime(ElapsedTime + RemainingTime) & " elapsed)<br>"
Response.write "&nbsp;Upload speed: " & Speed & " (" & SizeString(ReceivedBytes) & " of " & SizeString(TotalBytes) & " received)<br>"
End If
%>
</font>

<%
'########################
Function SizeString(size)
'########################
If Size>1024*1024 Then
SizeString=Round(Size/1024/1024,1) & "MB"
Else
SizeString=Round(Size/1024) & "kB"
End If
End Function

'########################
Function SecondsToTime(s)
'########################
t1=s
hr=s\3600
If len(hr)=s Then hr="0" & hr
s=s mod 3600
min=s\60
'If len(min)=1 Then min="0" & min
sec=s mod 60
If len(sec)=1 Then sec="0" & sec
If s>3600 Then SecondsToTime=hr & ":" & min & ":" & sec Else SecondsToTime= min & ":" & sec
End Function
%>