Restricting File Types


There are times when you may need to restrict the type of files saved on the Web server. You can do so by using FileUp's ContentTypeproperty and a Select condition to save only files that are a certain type.

Once your file is uploaded, you would include the following after creating an instance of FileUp:

Server-Side Code
<%
'--- Get the uploaded file's content-type and
'--- assign it to a variable.
FCONT = fileUpload.ContentType

'--- Use the Select Case Condition to restrict the file type.
Select Case LCase(FCONT)
Case "image/gif"
fileUpload.Save
Response.Write "<P>" & fileUpload.ShortFileName & " has been saved."

Case "image/pjpeg"
fileUpload.Save
Response.Write "<P>" & fileUpload.ShortFileName & " has been saved."

Case Else
fileUpload.delete
Response.Write "<P>" & "You can only upload gif and jpeg files.<br />"
Response.End
End Select
....
%>

We get the media content-type of the file uploaded by using the ContentTypeproperty. In this example, we accept only .gif and .jpeg files. So we are looking for either "image/pjpeg" or "image/gif" as the media content-type.

A Case condition statement is used to save the correct file and send a successful response. Notice that the property ShortFileNameis used to supply the name of the file that was saved. If the file type is incorrect, the file will be deleted and an error response will sent.


Summary

Top

Copyright © 2010 SoftArtisans, Inc. All rights reserved.