Search code examples
c#.netasp.netfile-upload

How to specify file extension in ASP.Net upload control?


Is there a way to specify a file extension for ASP.NET upload control? E.g. I want to upload images with jpg and png extensions only.

I can implement this easily for a windows application, but struggling with ASP.Net

Thank you


Solution

  • string[] validFileTypes = { "bmp", "jpg", "png" };
        string ext = Path.GetExtension(fileUpload1.FileName);
        bool isValidType = false;
    
        for (int i = 0; i < validFileTypes.Length; i++)
        {
            if (ext == "." + validFileTypes[i])
            {
                isValidType = true;
                break;
            }
        }
    
        if (!isValidType)
        {
            lblMessage.Text = "Invalid File Type";
        }
        else
        {
            lblMessage.Text = "File Uploaded Successfullty";
        }