Restricting File Type and Size Before Uploading


These examples shows how to restrict file types and set a maximum file size to upload. We'll use XFile instead of restricting at the server with FileUp. By restricting the file types on the client, the inappropriate files will not be uploaded at all. This saves from unneccesary bandwidth usage.

We'll need to parse through the filenames for the correct file extension.


Using XFile

Let's look at the first page:

The Client Code
<html>
<head>
<title>SoftArtisan XFile Restrict File Size and Type Upload Sample</title>
</head>

<body>
<strong>
<font color="black" size="5">
XFile File restrict Size and Type Upload Sample
</font>
</strong>

<object classid="CLSID:230C3D02-DA27-11D2-8612-00A0C93EEA3C"
id="AXFFile" name="AXFFile" style="left: 0px; top: 0px"
codebase="saxfile.cab" viewastext>
</object>

<input type="button" value="Button"
id="Button2" name="Button2" value="Start upload" />

<script language="vbs">

'-- This is where all the data will be sent
PostURL = "/SAfileUpSamples/ClientUpload/restrictTypeSize/SAXfile/formresp2.asp"

Set XFile = AXFFile.XFRequest
XFile.Server = "localhost"
XFile.ObjectName = PostURL
XFile.MaxFileSize = 500

AXFFile.AddFile "c:\boot.ini"
AXFFile.AddFile "C:\folder\file.jpg"

Sub Button2_onclick()
'-- Establish File Collection
'-- The files will upload but will not show up in the progress bar
Set files = AXFFile.Files

Dim ext(2)
ext(0) = "ini"
ext(1) = "txt"
ext(2) = "doc"

Dim fileDele
For Each p In files
For Each x In ext
fExt = Mid(p.FileName, InStrRev(p.FileName, ".") + 1)
fileDele = p.FileName
If StrComp(fExt, x) <> 0 Then
restCount = restCount + 1
End if
Next

If restCount = 3 Then ' number of extensions to restrict to
MsgBox(fileDele & " will be removed from the file list" & vbcrlf _
& "since it is not the correct file type")
AXFFile.Remove (p.FileName)
End If
restCount = 0
next

Call StartUpload

End Sub

Sub StartUpload()
winStyle = "height=300,width=400,status=no," & _
"toolbar=no,menubar=no,location=no"
Window.Open "AXFFileProgress.htm", "_blank", winStyle
AXFFile.Start()
End Sub

</script>
</body>
</html>

Let's look at the major sections of interest:

Copyright © 2010 SoftArtisans, Inc. All rights reserved.