0

我最近正在制作一个程序,可以让用户上传他们的肖像。所以我使用文件输入并重写onchange函数。在 onchange 函数中,我新建了一个图像对象,当图像对象准备好时,我使用图像的宽度和高度以及 src 来设置 img 的拟合宽度和高度。下面的代码在 chrome 中运行良好,但在 ie10 中失败。addEventListener 和 attachEvent 在 ie10 中都失败了。我只想让 img 处理 onload 事件。我怎样才能做到这一点?

function setImage(file){
var tempImg = document.createElement('img');
tempImg.addEventListener('load', function(){
        alert('success');

        var isExist = false;
        var imgDiv = document.getElementsByClassName('img-border')[0];

        if (document.getElementsByClassName('photo-canvas').length == 1){
            var canvas = document.getElementsByClassName('photo-canvas')[0];            
            isExist = true;
        }
        else{
            var canvas = document.createElement('canvas');
        }

        var xTimes = tempImg.width / portraitWidth;
        var yTimes = tempImg.height / portraitWidth;

        var realTimes = xTimes > yTimes ? xTimes : yTimes;

        realWidth = tempImg.width / realTimes;
        realHeight = tempImg.height / realTimes;

        centerX = realWidth / 2;
        centerY = realHeight / 2;

        var realImg = document.getElementsByClassName('temp-photo')[0];

        realImg.style.width = realWidth + 'px';
        realImg.style.height = realHeight + 'px';
        realImg.style.marginLeft = -realWidth / 2 + 'px';
        realImg.style.marginTop = -realHeight / 2 + 'px';
        realImg.src = null;
        realImg.src = tempImg.src;

        canvas.width = realWidth;
        canvas.height = realHeight;     
        canvas.style.marginLeft =  -realWidth / 2 + 'px';
        canvas.style.marginTop = -realHeight / 2 + 'px';

        if (!isExist){
            canvas.onload = function(){
                draw(this);
            }
            imgDiv.appendChild(canvas);
        }
        else
            draw(canvas);           
    }, false); 

if (document.all){
    tempImg.src = file.value;
    var temp = document.getElementsByClassName('temp-photo')[0];
    temp.src = file.value;
}
else{
    var image = file.files.item(0);

    if (typeof(image.getAsDataURL) != 'undefined')  
        tempImg.src = image.getAsDataURL();
    else
        tempImg.src = window.webkitURL.createObjectURL(image);
}       

}

4

1 回答 1

-1

如果要在 ie 中上传本地文件,则必须将该站点添加到受信任列表中。

于 2013-01-27T10:32:32.103 回答