How can I restrict the file type in the file browse menu of an AsyncFileUpload in the ASP.NET AJAX Control Toolkit

SteveGSD picture SteveGSD · Jul 28, 2010 · Viewed 8.2k times · Source

I would like to restrict what they see in the file upload dialog, which is set to "All Files" by default. I understand how to validate that they only uploaded a certain file type, that is not the question here. I would just like to know how to default the file type in the file selection dialog.

Is there any way to change this to "PNG only" or "*.png"?

This is using AsyncFileUpload in the ASP.NET AJAX Control Toolkit.

Answer

msaiz picture msaiz · Jun 4, 2012

This one is working for me (Thanks DavRob for the inspiration).

<cc1:AsyncFileUpload ID="FileUpload" runat="server" 
  OnClientUploadStarted="AssemblyFileUpload_Started" />

<script>
function AssemblyFileUpload_Started(sender, args) {
    var filename = args.get_fileName();
    var ext = filename.substring(filename.lastIndexOf(".") + 1);
    if (ext != 'png') {
        throw { 
            name:        "Invalid File Type", 
            level:       "Error", 
            message:     "Invalid File Type (Only .png)",
            htmlMessage: "Invalid File Type (Only .png)" 
        }
        return false;
    }
    return true;
}
</script>