input file 图片预览(项目摘抄)

/* 火狐下取本地全路径 */
function getFullPath(obj)
{
    if(obj)
    {
        //ie
        if (window.navigator.userAgent.indexOf("MSIE")>=1)
        {
            obj.select();
            if(window.navigator.userAgent.indexOf("MSIE") == 25){
                obj.blur();
            }
            return document.selection.createRange().text;
        }
        //firefox
        else if(window.navigator.userAgent.indexOf("Firefox")>=1)
        {
            if(obj.files)
            {
                //return obj.files.item(0).getAsDataURL();
                return window.URL.createObjectURL(obj.files.item(0));
            }
            return obj.value;
        }
        return obj.value;
    }
}
$(':file').each(function(idx,dom) {
    var el = $(dom);
    el.change(function(){
        var sub = el.parent().parent().parent().siblings(":first");
        var img = sub.find("img");
        var src = getFullPath(el[0]);
        alert(src);
        img.attr('src', src);
    });
});

 

posted @ 2014-09-24 16:15  泥称  阅读(178)  评论(0)    收藏  举报