Link to home
Start Free TrialLog in
Avatar of Parag_t567
Parag_t567Flag for India

asked on

height and width validation of image by using javascript

Hi All,

I have a upload area in which user is uploading a image and at that point i want to check whether the image is of the specific dimensions are not? If not then i want to throw and error.

Any help will be appreciated.

Thanks
Parag
Avatar of VeganBen
VeganBen

Nope. Can't be done in a pure client side environment.
Avatar of Parag_t567

ASKER

may be from the backend?
SOLUTION
Avatar of VeganBen
VeganBen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some version of IE and some old old versions of other browsers may have given you full file path on the client side, but these days it is looked at as a security issue, so almost all browsers and not allowing it.

Anyway, if you are using asp.net, here is a way to check file size on the backend.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUploadControl" runat="server" />
        <asp:Button ID="btUpload" runat="server" Text="Upload Image" OnClick="UploadFile" />
        <asp:Label ID="lbErrorMessage" runat="server" ForeColor="Red" />
    </div>
    </form>
</body>
</html>

using System.IO;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void UploadFile(object sender, EventArgs e)
{
    byte[] filebytes = FileUploadControl.FileBytes;
    MemoryStream stream = new MemoryStream(filebytes);
    System.Drawing.Image img = System.Drawing.Image.FromStream(stream);

    int height = img.Height;
    int width = img.Width;

    //Lets say we want an image that is 500x500 or less
    if (height <= 500 && width <= 500)
    {
        //Save file
    }
    else
    {
        lbErrorMessage.Text = "Error: The size of the image you selected exceeds the 500x500 limit.";
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please share your findings
thanks
I have already posted it...
Ah, I do not think I understand the solution, it looks like server side php?
But is does solved my propose. I don't mind giving points to anyone. Let the admin decide. I am cool with anything.

Thanks for your help :)

Thanks
Parag
It is not about points in this case, just wanted to know what the solution was.
So you solved it on the server, right?
Yes right.
Parag_t567,

I'll go ahead and close this question now, by accepting your own solution, and awaring points to the expert post that suggested doing this on the server side, and the expert post providing an alternative client side approach.

modus_in_rebus
Community Support Moderator