1.11M Members

Finding Width and Height of an image before uploading

 
0
 

I want to get the width and height of an image before uploading using javascript.
I used The code shown below.
But its returning 0 value

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function form_image()
{   

var v=new Image();
v.src=document.getElementById("f1").getAttribute('src');
alert(v.height+"-"+v.width);
}
</script>
</head>

<body>



<form method="post" enctype="multipart/form-data">
<input type="file" id="f1" name="f1" onchange="form_image()"/>
<input  type="submit" />
</form>
</body>
</html>
 
0
 

I think you need something like this:

var uploadedFile = document.getElementById('f1');
var fileHeight = uploadedFile.files[0].height;
 
0
 

I tried following code

function form_image1()
{   

    var uploadedFile = document.getElementById('f1');
    var fileHeight = uploadedFile.files[0].height;
    alert("fileHeight="+fileHeight);
}

Its getting output as "undefined "

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article