Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello Sir,
I want to upload image in jpg format to display photo as well as signature besides it to validate the photo and image that it take only photo and image to particular region.

Please give me code as earlier as possible. Help me
Posted
Updated 9-Nov-11 23:13pm
v3
Comments
Legor 10-Nov-11 4:44am    
You know the purpose of this site isn't to "give out code asap" like you asked for so nicely. It's more for helping people who at least attempt to do some work on their own.
Pandya Anil 10-Nov-11 5:36am    
if he did the rude behaviour, you too.. after advising so much... answer him otherwise you are too in the same category, who just advises people for their bad habits.
Smithers-Jones 10-Nov-11 5:14am    
Reason for my vote of 1: Asking for complete code without showing any effort, urging others to do his work.

1 solution

you have two choices.. validate at client side or at server side.

1) from server side

C#
//checks if it is an image
                private bool IsImage(byte[] data)
                {
                        //read 64 bytes of the stream only to determine the type
                        string myStr = System.Text.Encoding.ASCII.GetString(data).Substring(0,16);
                        //check if its definately an image.
                        if(myStr.Substring(8,2).ToString().ToLower()!="if")
                        {
                                //its not a jpeg
                                if(myStr.Substring(0,3).ToString().ToLower() != "gif")
                                {
                                        //its not a gif
                                        if(myStr.Substring(0,2).ToString().ToLower() != "bm")
                                        {
                                                //its not a .bmp
                                                if(myStr.Substring(0,2).ToString().ToLower() != "ii")
                                                {
                                                        //its not a tiff
                                                        //ProcessErrors("notImage");
                                                        this.myFile.PostedFile.InputStream.Close();
                                                        myStr = null;
                                                        return false;
                                                }
                                        }
                                }
                        }
                        myStr = null;
                        return true;
                }



2) from client side (Java Script)

JavaScript
<script language="javascript"> 
     function ValidateFile(source, args) 
     { 
        try 
         {        
        var fileAndPath= 
           document.getElementById(source.controltovalidate).value; 
        var lastPathDelimiter=fileAndPath.lastIndexOf("\\"); 
        var fileNameOnly=fileAndPath.substring(lastPathDelimiter+1);        
        var file_extDelimiter=fileNameOnly.lastIndexOf("."); 
        var file_ext=fileNameOnly.substring(file_extDelimiter+1).toLowerCase(); 
        if(file_ext!="jpg") 
             { 
             args.IsValid = false; 
             if(file_ext!="gif") 
               args.IsValid = false; 
                  if(file_ext!="png") 
                  { 
                    args.IsValid = false; 
                     return; 
                  } 
                } 
        }catch(err) 
        { 
          txt="There was an error on this page.\n\n"; 
          txt+="Error description: " + err.description + "\n\n"; 
          txt+="Click OK to continue.\n\n"; 
          txt+=document.getElementById(source.controltovalidate).value; 
          alert(txt); 
          } 
          
           args.IsValid = true; 
    } 
    </script>


ASP.NET
<asp:fileupload id="FileUpload1" runat="server" xmlns:asp="#unknown" /> 
 
<asp:customvalidator id="CustomValidator1" xmlns:asp="#unknown">
ClientValidationFunction="ValidateFile"  runat="server"  
ControlToValidate="FileUpload1"  
Display="dynamic" ErrorMessage="images only "> 



mark as answer if solves your problem
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900