Start New Discussion within our Web Development Community

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>

I think you need something like this:

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

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 "

This article has been dead for over six months. Start a new discussion instead.