The AsyncFileUpload control (part of the AjaxControlToolkit) does not expose a method that enables you to limit or filter the types of files that can be uploaded.
You could add logic to the server side event UploadedComplete to check the file extension prior to saving the file but you would need to upload the file before it could be checked.
Instead perform the test in the client side event OnClientUploadStarted. At first I thought that returning ‘false’ from this function would prevent the file uploading, it doesn’t. You need to raise an error within this function to stop the file upload. When the error occurs the OnClientUploadError function is called (if you have created a handler for this event).
<head>
<script type="text/javascript">
function uploadComplete(sender,args)
{
var file = args.get_fileName();
// Do something here to indicate the file has been uploaded.
}
function uploadStarted(sender,args)
{
var file = args.get_fileName();
// File types that should not be uploaded.
var extensions = "exe|bat|com|msi";
if (new RegExp("("+extensions+")$").test(file))
{
throw(new Error());
}
}
function uploadError(sender,args)
{
// Do something to indicate error
}
</script>
<script runat="server">
void afu_UploadComplete(object sender,AsyncFileUploadEventArgs e)
{
// Save File
}
void afu_UploadFileError(object sender,AsyncFileUploadEventArgs e)
{
// Handle Error
}
</script>
</head>
<body>
<form runat="server>
<ajx:ToolkitScriptManager runat="server" id="sm1"/>
<ajx:AsyncFileUpload runat="server" id="afu"
OnUploadComplete="afu_UploadComplete"
OnUploadFileError="afu_UploadFileError"
OnClientUploadStarted="uploadStarted"
OnClientUploadComplete="uploadComplete"
OnClientUploadError="uploadError"
UploaderStyle="Modern"
/>
</form>
</body>
