i have the following server validation in custum validator event
//create an array of valid file extensions
string[] validExt = { "jpg", "jpeg", "png", "gif", "bmp"};
//set args to invalid
args.IsValid = false;
//get file name
string fileName = fupImage.PostedFile.FileName;
//check to make sure the string is not empty
if (!string.IsNullOrEmpty(fileName))
{
//get file extension
string fileExt = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();
//check if current extensions matches any valid extensions
for (int i = 0; i < validExt.Length; i++)
{
if (fileExt == validExt[i]) //if we find a match
args.IsValid = true; //validate it
}
}
and Button click event to upload image:
//create an array of valid file extensions
string[] validExt = { "jpg", "jpeg", "png", "gif", "bmp"};
//set args to invalid
args.IsValid = false;
//get file name
string fileName = fupImage.PostedFile.FileName;
//check to make sure the string is not empty
if (!string.IsNullOrEmpty(fileName))
{
//get file extension
string fileExt = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();
//check if current extensions matches any valid extensions
for (int i = 0; i < validExt.Length; i++)
{
if (fileExt == validExt[i]) //if we find a match
args.IsValid = true; //validate it
}
}
//create an array of valid file extensions
string[] validExt = { "jpg", "jpeg", "png", "gif", "bmp"};
//set args to invalid
args.IsValid = false;
//get file name
string fileName = fupImage.PostedFile.FileName;
//check to make sure the string is not empty
if (!string.IsNullOrEmpty(fileName))
{
//get file extension
string fileExt = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();
//check if current extensions matches any valid extensions
for (int i = 0; i < validExt.Length; i++)
{
if (fileExt == validExt[i]) //if we find a match
args.IsValid = true; //validate it
}
}
//check to make sure a file is selected before adding it to the db
if (fupImage.PostedFile != null && !string.IsNullOrEmpty(fupImage.PostedFile.FileName) && CustomValidator1.IsValid)
{
//create a byte array to store the binary image data
byte[] imgBin = new byte[fupImage.PostedFile.ContentLength];
//store the currently selected file in memeory
HttpPostedFile img = fupImage.PostedFile;
//set the binary data
img.InputStream.Read(imgBin, 0, (int)fupImage.PostedFile.ContentLength);
//connect to the db
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//sql command to save our image data to the db
SqlCommand cmd = new SqlCommand("INSERT INTO Images(ImgBin, ImgSize, ImgType, ImgDate, ImgMessage) VALUES (@ImgBin, @ImgSize, @ImgType, @ImgDate, @ImgMessage)", conn);
cmd.CommandType = CommandType.Text;
//add img binary data
cmd.Parameters.Add("@ImgBin", SqlDbType.Image, imgBin.Length).Value = imgBin;
//add img size
cmd.Parameters.AddWithValue("@ImgSize", imgBin.Length);
//add img type
cmd.Parameters.AddWithValue("@ImgType", fupImage.PostedFile.ContentType);
//add img date
cmd.Parameters.AddWithValue("@ImgDate", DateTime.Parse(txtDate.Text));
//add img message
cmd.Parameters.AddWithValue("@ImgMessage", txtMessage.Text);
using (conn)
{
//open the connection
conn.Open();
//send the sql query to store our data
cmd.ExecuteNonQuery();
}
}
i need to make validation if the extension of the image is not valid
You have to wrap your button click event in a Page.IsValid or it doesn't matter you set the IsValid argument on the custom validator control.
So:
// Button Click
if (Page.IsValid)
{
// Do your upload...no need to revalidate here
// so take out that args.IsValid stuff
}
// Server Validate event
// What you have looks ok. Let me know if you have trouble.
aimn20034
Member
13 Points
31 Posts
Problem in Custom validator event
Mar 02, 2012 07:46 PM|LINK
i have the following server validation in custum validator event
//create an array of valid file extensions string[] validExt = { "jpg", "jpeg", "png", "gif", "bmp"}; //set args to invalid args.IsValid = false; //get file name string fileName = fupImage.PostedFile.FileName; //check to make sure the string is not empty if (!string.IsNullOrEmpty(fileName)) { //get file extension string fileExt = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower(); //check if current extensions matches any valid extensions for (int i = 0; i < validExt.Length; i++) { if (fileExt == validExt[i]) //if we find a match args.IsValid = true; //validate it } } and Button click event to upload image://create an array of valid file extensions string[] validExt = { "jpg", "jpeg", "png", "gif", "bmp"}; //set args to invalid args.IsValid = false; //get file name string fileName = fupImage.PostedFile.FileName; //check to make sure the string is not empty if (!string.IsNullOrEmpty(fileName)) { //get file extension string fileExt = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower(); //check if current extensions matches any valid extensions for (int i = 0; i < validExt.Length; i++) { if (fileExt == validExt[i]) //if we find a match args.IsValid = true; //validate it } } //create an array of valid file extensions string[] validExt = { "jpg", "jpeg", "png", "gif", "bmp"}; //set args to invalid args.IsValid = false; //get file name string fileName = fupImage.PostedFile.FileName; //check to make sure the string is not empty if (!string.IsNullOrEmpty(fileName)) { //get file extension string fileExt = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower(); //check if current extensions matches any valid extensions for (int i = 0; i < validExt.Length; i++) { if (fileExt == validExt[i]) //if we find a match args.IsValid = true; //validate it } } //check to make sure a file is selected before adding it to the db if (fupImage.PostedFile != null && !string.IsNullOrEmpty(fupImage.PostedFile.FileName) && CustomValidator1.IsValid) { //create a byte array to store the binary image data byte[] imgBin = new byte[fupImage.PostedFile.ContentLength]; //store the currently selected file in memeory HttpPostedFile img = fupImage.PostedFile; //set the binary data img.InputStream.Read(imgBin, 0, (int)fupImage.PostedFile.ContentLength); //connect to the db SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); //sql command to save our image data to the db SqlCommand cmd = new SqlCommand("INSERT INTO Images(ImgBin, ImgSize, ImgType, ImgDate, ImgMessage) VALUES (@ImgBin, @ImgSize, @ImgType, @ImgDate, @ImgMessage)", conn); cmd.CommandType = CommandType.Text; //add img binary data cmd.Parameters.Add("@ImgBin", SqlDbType.Image, imgBin.Length).Value = imgBin; //add img size cmd.Parameters.AddWithValue("@ImgSize", imgBin.Length); //add img type cmd.Parameters.AddWithValue("@ImgType", fupImage.PostedFile.ContentType); //add img date cmd.Parameters.AddWithValue("@ImgDate", DateTime.Parse(txtDate.Text)); //add img message cmd.Parameters.AddWithValue("@ImgMessage", txtMessage.Text); using (conn) { //open the connection conn.Open(); //send the sql query to store our data cmd.ExecuteNonQuery(); } }i need to make validation if the extension of the image is not valid
adamturner34
Contributor
4394 Points
1102 Posts
Re: Problem in Custom validator event
Mar 02, 2012 07:56 PM|LINK
Ok,
Instead of all that looping, you can either use .IndexOf for the array or contains to validate existence.
Secondly, what is the problem?
aimn20034
Member
13 Points
31 Posts
Re: Problem in Custom validator event
Mar 02, 2012 08:02 PM|LINK
when i upload invalid image , invalid omage is displayed
i need instead of that, diplaying Error message of validator
ny2244111
Member
184 Points
34 Posts
Re: Problem in Custom validator event
Mar 03, 2012 02:04 AM|LINK
You have to wrap your button click event in a Page.IsValid or it doesn't matter you set the IsValid argument on the custom validator control.
So:
// Button Click if (Page.IsValid) { // Do your upload...no need to revalidate here // so take out that args.IsValid stuff } // Server Validate event // What you have looks ok. Let me know if you have trouble.