Return to Snippet

Revision: 35736
at November 13, 2010 03:26 by housecor


Updated Code
public void ValidatePhotoUpload(object sender, ServerValidateEventArgs e)
		{
			string filename = ((FileUpload)Page.Master.FindControl("ContentPlaceHolder1").FindControl("fuPhoto")).FileName;
			CustomValidator validator = ((CustomValidator)sender);

			//if file wasn't uploaded at all, exit.
			if (filename.Length == 0) return;

			//make sure the file uploaded is of an accepted type
			if (!ValidFileType(filename))
			{
				e.IsValid = false;
				string errorMessage = "Only the following filetypes are accepted: ";
				
				foreach (string extension in ValidFileExtensions)
				{
					errorMessage += extension + " ";
				}

				validator.ErrorMessage = errorMessage;
				return;
			}

			//make sure the file is under the size limit
			int filesizeInBytes = ((FileUpload)Page.Master.FindControl("ContentPlaceHolder1").FindControl("fuPhoto")).PostedFile.ContentLength;
			const int maxFileSizeInMB = 4;
			const int maxFileSizeInBytes = maxFileSizeInMB * 1000000;
			if (filesizeInBytes > maxFileSizeInBytes)
			{
				e.IsValid = false;
				validator.ErrorMessage = "The max file size for uploaded files is " + maxFileSizeInMB + "MB.";
				return;
			}

			//make sure a file doesn't already exist on the server with the same name
			int numFilesWithSameName = (from f in db.TeamMembers
										where f.Photo == filename
										select f).Count();
			if (numFilesWithSameName > 0)
			{
				validator.ErrorMessage = "A file already exists with the name " + filename + ". Please rename the file and upload it again.";
				e.IsValid = false;
				return;
			}

			//validate photo aspect ratio is 8x10 for consistency
			System.Drawing.Image file = System.Drawing.Image.FromStream(fuPhoto.PostedFile.InputStream);
			float aspectRatio = ((float)file.Height / file.Width);
			if (aspectRatio != 1.25)
			{
				validator.ErrorMessage = "For consistency, team member photos should be a head shot with an aspect ratio of 8x10. For example, if you upload an image that is 1000 pixels tall, it must be 800 pixels wide. Please crop the image to 8x10 and reupload.";
				e.IsValid = false;
				return;
			}
			
			//validate photo meets minimum dimsension requirements
			if (file.Width < MemberPhotoWidth || file.Height < MemberPhotoHeight)
			{
				validator.ErrorMessage = "The photo must be at least " + MemberPhotoHeight + " pixels tall and " + MemberPhotoWidth + " pixels wide.";
				e.IsValid = false;
				return;
			}

			e.IsValid = true;
		}

Revision: 35735
at November 12, 2010 12:16 by housecor


Initial Code
public void ValidatePhotoUpload(object sender, ServerValidateEventArgs e)
		{
			string filename = ((FileUpload)Page.Master.FindControl("ContentPlaceHolder1").FindControl("fuPhoto")).FileName;
			CustomValidator validator = ((CustomValidator)sender);

			//if file wasn't uploaded at all, exit.
			if (filename.Length == 0) return;

			//make sure the file uploaded is of an accepted type
			if (!ValidFileType(filename))
			{
				e.IsValid = false;
				string errorMessage = "Only files of the following type are accepted: ";
				
				foreach (string extension in ValidFileExtensions)
				{
					errorMessage += extension + " ";
				}

				validator.ErrorMessage = errorMessage;
				return;
			}

			//make sure the file is under the size limit
			int filesizeInBytes = ((FileUpload)Page.Master.FindControl("ContentPlaceHolder1").FindControl("fuPhoto")).PostedFile.ContentLength;
			const int maxFileSizeInMB = 4;
			const int maxFileSizeInBytes = maxFileSizeInMB * 1000000;
			if (filesizeInBytes > maxFileSizeInBytes)
			{
				e.IsValid = false;
				validator.ErrorMessage = "The max file size for uploaded files is " + maxFileSizeInMB + "MB.";
				return;
			}

			//make sure a file doesn't already exist on the server with the same name
			int numFilesWithSameName = (from f in db.TeamMembers
										where f.Photo == filename
										select f).Count();
			if (numFilesWithSameName > 0)
			{
				validator.ErrorMessage = "A file already exists with the name " + filename + ". Please rename the file and upload it again.";
				e.IsValid = false;
				return;
			}

			//validate photo aspect ratio is 8x10 for consistency
			System.Drawing.Image file = System.Drawing.Image.FromStream(fuPhoto.PostedFile.InputStream);
			float aspectRatio = ((float)file.Height / file.Width);
			if (aspectRatio != 1.25)
			{
				validator.ErrorMessage = "For consistency, team member photos should be a head shot with an aspect ratio of 8x10. For example, if you upload an image that is 1000 pixels tall, it must be 800 pixels wide. Please crop the image to 8x10 and reupload.";
				e.IsValid = false;
				return;
			}
			
			//validate photo meets minimum dimsension requirements
			if (file.Width < MemberPhotoWidth || file.Height < MemberPhotoHeight)
			{
				validator.ErrorMessage = "The photo must be at least " + MemberPhotoHeight + " pixels tall and " + MemberPhotoWidth + " pixels wide.";
				e.IsValid = false;
				return;
			}

			e.IsValid = true;
		}

Initial URL

                                

Initial Description

                                

Initial Title
Validate photo file upload

Initial Tags

                                

Initial Language
C#