Click here to Skip to main content
16,018,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a three fileupload controls only for images in my aspx page.. I have validate that file is image or not. Now i need to check that image dimensions must be 640*480 in every fileupload controls... How can i do check that dimensions are 640*480 before page postback to server?
Posted

Unfortunately, you cannot determine the dimensions of a file at the client side (well, you can't without parsing the byte array into the image file format manually - and that's probably not what you're after). What you could do (assuming you're using HTML 5), is post the file over to the server using AJAX and the File API, and have the file decode there.

One trick you could use, and this is something I talked about as a way of handling another problem, yesterday[^] is to use an HttpHandler to do the checking for you. Basically, you would post the image as byte data across to the HttpHandler which would decode the image into a physical image and test it in a similar way to Krunal Rohit's code, and then return an appropriate value if it can be uploaded properly.

The downside of this approach, of course, is that you are posting the data to the server twice, but there are techniques you could use to mitigate that (such as storing the uploaded image in the Session object).
 
Share this answer
 
v2
C#
System.Drawing.Image imgFile =
System.Drawing.Image.FromStream(FileUploader1.PostedFile.InputStream);
  if (imgFile.PhysicalDimension.Width < 640 || imgFile.PhysicalDimension.Height < 280)
  {
      Reponse.Write("<script>alert('invalid iamge')</script>")
  }


I haven't tried out this code yet but it is my blind assumption that it should work,
so give it a try and if it works go for other FileUpload control !

Good Luck !
 
Share this answer
 
Comments
Pete O'Hanlon 23-Jan-14 9:29am    
That requires a post to the server before it can determine whether or not the image matches the dimensions.
Krunal Rohit 23-Jan-14 9:31am    
As I said I have not tried this code yet, but yeah what we can do is, we can do this using JavaScript !
Pete O'Hanlon 23-Jan-14 9:41am    
It's still going to run server side. There's no way around the file having to be transferred over to the server to test it. And yes, your code will put out an alert, but that alert will have been written back from the server.
Krunal Rohit 23-Jan-14 9:46am    
Yes, you're right. Still its going to work on server side.
SnvMohan 23-Jan-14 22:26pm    
I need a javascript for validate this..... i have code like this on button click event but i don't want use this..I need javascript for this...Help me plz?

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