Fileupload Control To Take .jpg Only

Fileupload control in asp.net is used to upload different file from your computer to server. In the previous article you come to know about how we can limit the size of fileupload control.

Now in this article we restrict fileupload control to take only ¬†“.jpg” format images.

string extfileupload = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName);
if (extfileupload.ToUpper().Trim() != “.JPG” || extfileupload.ToLower().Trim() != “.jpg”)
{
ClientScript.RegisterStartupScript(this.GetType(), “myalert”, “alert(‘Image format should be in .jpg only’);”, true);
}
else
{

// your code to upload image

 

}

By that way we find first the extension of file and store it in string then we make condition for that image extension.

Leave a Reply