0

I think ajaxcontroltoolkit:ajaxfileupload has too many bugs and wane. since I'm using this component frequently in my projects, I overcome to many serious disturbing bugs and functionality of this control. Now, I seriously need to get uploaded image width and height using ajaxfileupload before I save it to check either width or height are correct and based on retrieved information, informing users in case of image width and height are not compatible and then prevent the process to go further and save the picture. Any Ideas please?!

HTML Side:

<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
        onuploadcomplete="AjaxFileUpload1_UploadComplete" ThrobberID="myThrobber" MaximumNumberOfFiles="1" AllowedFileTypes="jpg,jpeg"/>

Behind Code:

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    string filePath = "~/upload/" + e.FileName;
    AjaxFileUpload1.SaveAs(filePath);

}
leppie
  • 115,091
  • 17
  • 196
  • 297
Hooman
  • 1,775
  • 1
  • 16
  • 15

1 Answers1

6

AjaxFileUpload just handles the upload but doesn't give you any information about the file itself. You need to use something like

System.Drawing.Image objImage = System.Drawing.Image.FromFile(Server.MapPath(filePath));
width = objImage.Width;
height = objImage.Height; 

If you do not want to load the file into memory, you can check How do I reliably get an image dimensions in .NET without loading the image? or Getting image dimensions without reading the entire file.

An alternative would be to check image width and height on client side, using a tool like Plupload or a similar one.

Community
  • 1
  • 1
Olaf
  • 10,049
  • 8
  • 38
  • 54
  • Thanks. but, don't u think that it makes some overload on server side? – Hooman Dec 04 '12 at 04:43
  • Why don't you jut try and see if it works? These are the only ways. The solutions in the links cost less memory and CPU but don't work for all image types. I have added an alternative, but then you wouldn't use AjaxFileUpload. – Olaf Dec 04 '12 at 06:38
  • Ok, mate... thank you very much... I'll give u update after I finished work around it. – Hooman Dec 05 '12 at 17:36