How would I tie this to the Custom Control Validator? Or is that even possible?
Main Topics
Browse All TopicsI'm creating a small photo gallery app and I want to validate the file so that only JPEG or GIF files can be uploaded. Can you guide me to how to do this? I would like to use the CustomValidator if possible, but if not, I just need guidance... A URL would be nice as a reference if available, I couldn't find anyting on Google
In case you need to know, I'm currently using System.IO.Stream to read the file and upload it into sql server database table
Thanks for your help in advance!
Happy New Year...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You could use a regular expression like the one here:
http://www.regexlib.com/RE
or
Check for the files extension and then proceed accordingly, heres a snippit of one of my image resizing classes to give you an idea of how this would be done.
Public Function ResizeImage(ByVal ImageSource As String) As String
Dim IncomingImage As System.Drawing.Image
Dim OutputBitmap As Bitmap
Dim JpegCodec As ImageCodecInfo
Dim JpegEncodeParams As EncoderParameters
Dim NewSize As Size
Dim fiSource As New IO.FileInfo(ImageSource)
Dim fsSource As IO.FileStream
Dim fiDestination As New IO.FileInfo(ImageSource)
Dim NewFilename As String
fiSource.MoveTo(fiSource.D
fsSource = fiSource.OpenRead()
IncomingImage = IncomingImage.FromStream(f
OutputBitmap = RedrawImage(IncomingImage,
If fiSource.Extension = ".gif" Then
NewFilename = fiDestination.Name
OutputBitmap.Save(fiDestin
Else
JpegCodec = ReturnJpegCodec()
JpegEncodeParams = ReturnEncoderParams()
NewFilename = fiDestination.Name.Replace
OutputBitmap.Save(fiDestin
JpegCodec, JpegEncodeParams)
End If
fsSource.Close() ''''This may have done it check on wednesday
'This is causing the generic GDI+ exception
If CreateThumbnail Then
OutputBitmap = RedrawImage(IncomingImage,
If fiSource.Extension = ".gif" Then
OutputBitmap.Save(fiDestin
Else
JpegCodec = ReturnJpegCodec()
JpegEncodeParams = ReturnEncoderParams()
OutputBitmap.Save(fiDestin
fiDestination.Name.Replace
End If
End If
OutputBitmap.Dispose()
IncomingImage.Dispose()
fiSource.Delete()
Return NewFilename
End Function
1) Client - Side Validation
You can use the following regular expression validator for file upload validation:
<asp:RegularExpressionVali
ErrorMessage="Only picture files are allowed!"
ValidationExpression="^(([
ControlToValidate="fileUpl
2) Server Side Validation
If you want to use Server side validation( As validators dont work in fastest FIREFOX browser) then Use server side checking.....
// Create posted file object :
private bool IsValidImageFile()
{
HttpPostedFile filePosted = fileUpload.PostedFile;
//check if its imageFile:
if (filePosted.ContentType.In
{return true; }
else
{ return false; }
}
3) How to use custom validator:
http://dotnetjunkies.com/W
4) Uploade file to SqlServer:
Theres a nice article on what are trying to do, Please do refere this...
http://www.codeproject.com
Cheeers!!!!
Maulik_M_Modi
Business Accounts
Answer for Membership
by: ayha1999Posted on 2004-12-30 at 10:31:03ID: 12927737
<input type="file" runat="server" id="fileUpload" />
PostedFile )) dexOf("ima ge") >= 0) //check if its imageFile: d.FileName );
t);//Save image ot server
-------Code behind file-> function for uploading image------
using System.IO;
void ImageUpload()
{
// Create posted file object :
HttpPostedFile filePosted = fileUpload.PostedFile;
if(File.Exists(fileUpload.
{
if (filePosted.ContentType.In
{
string strFileName = Path.GetFileName(filePoste
string strTarget = Server.MapPath("images") + "\\" + strFileName;
filePosted.SaveAs(strTarge
}
}
}
Make sure you have set Write permission for ASP.NET user on production server to write into "images" folder.
ayha1999