tags:

views:

34

answers:

1

I'd like to get both C# and VB.NET sugestion.

I have an ASP.NET FileUpload control, which allows only image type. I used RegularExpressionValidator like below.

            <asp:FileUpload ID="fuPhoto" runat="server" TabIndex="40" />
        &nbsp;<asp:RegularExpressionValidator ID="RegularExpressionValidator3" 
                runat="server" ControlToValidate="fuPhoto" Display="Dynamic" 
                ErrorMessage="* You can only upload .jpg, .gif or .png image types." 
                ValidationExpression="^.*\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG)$">* You can only upload .jpg, .gif or .png image types.</asp:RegularExpressionValidator>

This method will only verify the file's extension, not its actual type. Once I receive the file, I want to examine its contents to determine what it really is, in this case image only.

How do I examine the file content being uploaded?

A: 

I think that regular expression is wrong because it prevents me uploading jpg's. In any case you can check the content type on the server using something like:

    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (string filename in Request.Files)
            Response.Write("<p>" + Request.Files[filename].ContentType + "</p>");
    }

Refer to http://en.wikipedia.org/wiki/Internet_media_type for valid types

Tim Carter
You're right, I corrected the regex.
Narazana