[RESOLVED]Validate image file type

If the following validates the file size; what would be the line for file type?

if (Request.Files.Count < 250) { }


if (Request.Files.Count
< 250)
{ }

That just ensures that less than 250 files were uploaded. ContentLength gives you the size in bytes. You can get the extension from Path.GetExtension(filename):

var allowedExtensions = new[]{".png", ".gif", ".jpg" };
var uploadedFile = Request.Files[0];
var fileName = Path.GetFileName(uploadedFile.FileName);
var extension = Path.GetExtension(fileName);
    //file meets the allowed type

Thanks for pointing that out. Then, how does ContentLength is being used?

I dont’ think it is referred to as follows

if (Request.Files.ContentLength  < 512000) { }

Hope this example will help you

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
        // Clear the BulletedList.

        // Check to see if at least one file was specified.
        if (FileUpload1.HasFile | FileUpload2.HasFile)
            Label1.Text = "The file collection consists of:";

            // Get the HttpFileCollection.
            HttpFileCollection hfc = Request.Files;
            foreach (String h in hfc.AllKeys)
                // Add an item to the BulletedList if a file
                // was specified for the corresponding control.
                if (hfc[h].ContentLength > 0) // You can check content type by hfc[h].ContentType

            Label1.Text = "You did not specify any files to upload or " +
                "the file(s) could not be found.";


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpFileCollection Example</title>
    <form id="form1"
        <asp:FileUpload ID="FileUpload1"
                        runat="server" />
        <br />
        <asp:FileUpload ID="FileUpload2"
                        runat="server" />
        <br />
        <asp:Button ID="Button1"
                    Text="Upload" />
        <br />
        <asp:Label ID="Label1"
        <br />
        <asp:BulletedList ID="BulletedList1"

Thanks but I am using Razor syntax.

Request.Files is a collection. You reference individual files by their zero-based index in that collection, so the first file is Request.Files[0]. 

var file = Request.Files[0];
var contentLength = file.ContentLength;

I can’t get it to work. It only returns an error if I specify if (Request.Files[0].ContentLength < 0) anything greater that ZERO uploads the file

                          if (Request.Files[0].ContentLength < 0) {
                            newFileName = vbliduser +  vblextension;
                            photo.Resize(width: 300, height: 300, preserveAspectRatio: true, preventEnlarge: true);
                            photo.Save(@"~images" + newFileName);
                               vblerror ="1";
                        catch(HttpException ex){

The file will always be uploaded. The only way you can prevent that is if you use some kind of Flash or Silverlight based client-side upload which can getthe file’s length in the browser. You can set a maxRequestLength value in web.config (by default it’s
4MB) to cause an HttpException at a lower value than the default, but that still relies on the file being fully uploaded. 

Leave a Reply