Restricting File Type and Size Before Uploading

This example shows how to restrict file types and set a maximum file size to upload. We'll use SA-JFile and SA-XFile, Software Artisans' client-side components, instead of restricting at the server level. By restricting the file types on the client side, the inappropriate files will not be uploaded at all.

In both components we'll need to parse, or search through the filenames for the correct file extension. Establishing the maximum size usually means setting a property for the component.

Using SA-JFile

To correctly illustrate the means for restricting the file type, we'll disable the loading of the applet visually. We'll create code that will look through the file collection set before and after adding file(s). Then, we simply set a parameter within the applet tag itself.

Here is the code:

<%@ LANGUAGE="VBSCRIPT" %>
<%
	
	' Get the name of the host machine
	httpHost = Request.ServerVariables("HTTP_HOST") 

	' This is where all the data will be sent
	PostURL = "http://" & httpHost & "/SAfileUpSamples/ClientUpload/restrictTypeSize/JFile/formresp.asp"

%>
<HTML>
<HEAD>
<TITLE>
	SA-JFile Restrict File size and Type Upload Sample
</TITLE>
<script language="Javascript" >
<!--

function add()
{
	if (document.Form1.addFile.value != "")
	{
		document.fileupload.addFile(document.Form1.addFile.value);
		alert(document.Form1.addFile.value + " " + "was added.");
		}
	else   
		{
			alert("No file was added");
		}
	
	
}

function myTransferRestrict()
{
	
	if(SAJFileAppletLoaded)
	{
		
		var file;
		var ext =  new Array() ;
		
		file = document.fileupload.getNumberOfFiles();
		
		ext[0] = "ini"; 
		ext[1] = "txt";
		//ext[2] = "doc";
		
		//Check File Type using restrictFiles function
		restrictFiles(file,ext)
		
		document.fileupload.startUpload();
		
		
	}
	else
		alert("Applet is not loaded.");
}

function restrictFiles( file , ext){
		var typeRes;

		for(i = 0;i < file;i++){ 
			Filename = document.fileupload.getFileNumber(i);
			typeRes = 0
			for(y = 0;y < ext.length;y++){	
				if (Filename.indexOf(ext[y]) == -1){
					typeRes++
					if (typeRes == ext.length && Filename != "") {
						alert(Filename + " " + "will be removed from the file list, since it is not the correct file type.")
						document.fileupload.removeFile(Filename)
						file --;
						i --;
						
					}
				}
			}
			
		}
}


//-->
</script>

</HEAD>

<BODY>

<FORM>

</FORM>

The Java Applet contained in this page can point to files which are located on a users local disk. These default file names can be specified in the Java Applet using applet parameters. The user can also select files using the Java Applet interface.

A function is used to verify the file type by looking at the filename extension.

These files will be submitted to a URL specified as a parameter in the Java applet.

There are two files that have been preset - boot.ini and Autoexec.bat.

This applet will only except ini or doc files and has a file size limit of 1KB.


	<APPLET CODEBASE="/SAJFileSamples" code="softartisans.filetransfer.UploadClient.class" height="0" width="0" mayscript archive="filetransfer.jar" name="fileupload" VIEWASTEXT>
		<PARAM name="cabbase" value="filetransfer.cab">

		<!-- The PostURL is where all the files and form elements will be uploaded to -->
		<PARAM name="PostURL" value="<%=PostURL%>">
		<PARAM name="Filename1" value="c:\boot.ini">
		<PARAM name="Filename2" value="C:\autoexec.bat">
		<PARAM name="HideFileList" value="1">

		<!-- set maxsize-->
		<PARAM name="MaximumFileSize" value="1000">
		
	</APPLET>
<p>
<input type=button value="Verify File Types" onclick="myTransferRestrict()"
</p>

<Form name=Form1>
<BR>
Try to add a file that is not an INI or DOC file.
Add a file:<INPUT type="text" name="addFile">
<input type=button value="Add File" onclick="add()" name="addFilebt">
</Form>
</BODY>
</HTML>

Let's go over some major points of this page:

Most of the work in the example comes from getting the file collection from SA-JFile and searching for the file extensions to restricting the files to be uploaded.

Using SA-XFile

With SA-XFile, The file collection is more straight-forward, but the process is the same. The GUI is also visible.

Let's look at the first page:

<%@ LANGUAGE="VBSCRIPT" %>
<%
	
	' Get the virtual location of file from site 
	Filelocal = Server.MapPath("\SAFileUpSamples\ClientUpload\Restricttypesize\")  

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

%>
<HTML>
<HEAD>
<TITLE>Software Artisans SA-AXFile Restrict Size and Type Upload Sample</TITLE>
<%
'---Copyright (c) 1999, Software Artisans, Inc.
'---Mail: info@softartisans.com   http://www.softartisans.com
%>
</HEAD>
<BODY>
<P><STRONG><FONT color=black size=5>SA-XFile File restrict Size and Type Upload Sample</FONT></STRONG></P>
<P>

<OBJECT classid=CLSID:230C3D02-DA27-11D2-8612-00A0C93EEA3C id=AXFFile 
style="LEFT: 0px; TOP: 0px" codebase = saxfile.cab VIEWASTEXT>
</OBJECT>
</P>


<INPUT type="button" value="Button" id=button2 name=button2 value="Start upload" >
<script language="VBS">
	
    Set XFile = AXFFile.XFRequest
    XFile.Server = "localhost"
    XFile.ObjectName = PostURL
    XFile.MaxFileSize = 500
    
    AXFFile.AddFile "c:\boot.ini"
    AXFFile.AddFile "<%=Filelocal%>" & "\softartlogo.jpg"
	
	
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)
			'msgbox(FExt & " " & x)
			'msgbox(Strcomp(FExt,x))
			FileDele = p.filename
			if Strcomp(FExt,x) <> 0 Then
				
				RestCount = RestCount + 1
				
			end if
			
		next
			'msgbox("Count:" & RestCount)
			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",null,winstyle
	AXFFile.Start()
end sub

</SCRIPT>
</BODY>
</HTML>

Let's look at the major sections of interest:

<%
	
	' Get the virtual location of file from site 
	Filelocal = Server.MapPath("\SAFileUpSamples\ClientUpload\Restricttypesize\")  

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

%>
<OBJECT classid=CLSID:230C3D02-DA27-11D2-8612-00A0C93EEA3C id=AXFFile 
style="LEFT: 0px; TOP: 0px" codebase = "saxfile.cab" VIEWASTEXT>
</OBJECT>
<script language="VBS">
	
    Set XFile = AXFFile.XFRequest
    XFile.Server = "localhost"
    XFile.ObjectName = PostURL
    XFile.MaxFileSize = 500
  
    AXFFile.AddFile "c:\boot.ini"
    AXFFile.AddFile "<%=Filelocal%>"& "\softartlogo.jpg"

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)
			'msgbox(FExt & " " & x)
			'msgbox(Strcomp(FExt,x))
			FileDele = p.filename
			if Strcomp(FExt,x) <> 0 Then
				
				RestCount = RestCount + 1
				
			end if
			
		next
			'msgbox("Count:" & RestCount)
			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",null,winstyle
	AXFFile.Start()
end sub
</SCRIPT>
You can customize limitation properties, such as MaxBytes. If the limitation fails, the upload will not start. If one file exceeds the limit, no files are uploaded.

Now you can you can restrict file type to uploads and set the max size.

 

Previous Page Next Page