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:
- First we create the AXFFile object from the XFile cab.
<object classid="CLSID:230C3D02-DA27-11D2-8612-00A0C93EEA3C" id="AXFFile"
name="AXFFile" style="LEFT: 0px; TOP: 0px"
codebase="saxfile.cab" viewastext>
</object>
- We create a button that will be used for firing a subroutine for the restricting
of file types and calling the upload function - StartUpload()
<input type="button" value="Button" id="Button2" name="Button2" value="Start
upload" />
- Now we are establishing the properties that set up the XFile transfer. We call an
instance of the Request object and then set the Server. With ObjectName property,
we set the web page that will create the upload process. The MaxFileSize property
is where we set the max size to be uploaded. The error for the max size is intrinsic
to the component. We have set this to 500 bytes.
<script language="vbscript">
Set XFile = AXFFile.XFRequest
XFile.Server = "localhost"
XFile.ObjectName = PostURL
XFile.MaxFileSize = 500
- This is where we preset files to be uploaded.
AXFFile.AddFile "C:\boot.ini"
AXFFile.AddFile "C:\folder\softartlogo.jpg"
- This is the onclick event subroutine. This is where we create the file collection
and restrict the type by the extension. We first create an instance of the file
collection with the Files object. Then an array is created with all of the file
type extensions to which we want to restrict the upload. The FileDele variable is
set to the filename that will be deleted. There are two iterations that are used
for the extension array and the file collection. Within the second iteration we
get the extension of the file from the list. We set the variable, FileDele to the
file in the list and then compare the file extension and the extension from the
array created. If the comparison is not equal to 0, which mean they are the same,
then we add a count of the extension (RestCount variable) not matched. This will
iterate until all the extensions from the array are verified. If the count is equal
to the amount of extension matched, then the file is not one of the file types specified
and it is removed from the list. Each time the ResCount is set back to zero to reset
the process. We then go to the next file in the collection until there is no more.
After which the StartUpload subroutine is called.
Sub Button2_OnClick()
'--- Establish File Collection
'--- The files will upload but will not show up in the progress
bar
Set files = AXFFile.Files
'MsgBox("File count: " & files.Count)
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
- The startUpload subroutine sets the progress window that will be opened through
the transfer, and then transfer begins.
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>
Top
Copyright © 2010 SoftArtisans, Inc. All rights reserved.