Question

I am trying to show the image dimensions with other properties using the HTML5 file API For some reason I am getting height of my image as 0.

http://jsfiddle.net/NfDMh/3/

<input type="file" id="files" name="files[]" multiple="multiple" />
    <output id="list"></output>​

function handleFileSelect(evt)
{
    var files = evt.target.files; 
    var output = [];
    var height;
    var width;
    for (var i = 0, f; f = files[i]; i++)
    {
        output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
                    f.size / 1024, ' KB, last modified: ',
                    f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
                    '</li>');
        checkDimensions(f);

    }
    document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}

function checkDimensions(file)
{
    var _URL = window.URL || window.webkitURL;
    var Dimensions = new Object;
    image = new Image();
    image.src = _URL.createObjectURL(file);
    alert(image.height);

}

document.getElementById('files').addEventListener('change', handleFileSelect, false);
Was it helpful?

Solution

You need to hook into the onload event. You are trying to look at the size before the image has loaded.

image = new Image();
image.onload = function() { alert(image.size) };
image.src = _URL.createObjectURL(file);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top