【转】inputfile的本地图片预览+等比例缩放兼容IE8、火狐

摘要:
发件人。价值match){alert('无效的图片格式!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Firefox3,IE6,IE7,IE8上传图片预览</title>
<style type="text/css">
#preview_wrapper
{
    display
:inline-block;
    width
:300px;
    height
:300px;
    background-color
:#CCC;
}
#preview_fake
{ /* 该对象用户在IE下显示预览图片 */
    filter
:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);
}
#preview_size_fake
{ /* 该对象只用来在IE下获得图片的原始尺寸,无其它用途 */
    filter
:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);
    visibility
:hidden;
}
#preview
{ /* 该对象用户在FF下显示预览图片 */
    width
:300px;
    height
:300px;
}
</style>
<script type="text/javascript">
function onUploadImgChange(sender){
    if( !sender.value.match( /.jpg|.gif|.png|.bmp/i ) ){
        alert('图片格式无效!');
        return false;
    }
    var objPreview = document.getElementById( 'preview' );
    var objPreviewFake = document.getElementById( 'preview_fake' );
    var objPreviewSizeFake = document.getElementById( 'preview_size_fake' );
    if( sender.files && sender.files[0] ){
        objPreview.style.display = 'block';
        objPreview.style.width = 'auto';
        objPreview.style.height = 'auto';
        // Firefox 因安全性问题已无法直接通过 input[file].value 获取完整的文件路径
        objPreview.src = sender.files[0].getAsDataURL();
    }else if( objPreviewFake.filters ){
        // IE7,IE8 在设置本地图片地址为 img.src 时出现莫名其妙的后果
        //(相同环境有时能显示,有时不显示),因此只能用滤镜来解决
        // IE7, IE8因安全性问题已无法直接通过 input[file].value 获取完整的文件路径
        sender.select();
        var imgSrc = document.selection.createRange().text;
        objPreviewFake.filters.item(
            'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;
        objPreviewSizeFake.filters.item(
            'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;
        autoSizePreview( objPreviewFake,
            objPreviewSizeFake.offsetWidth, objPreviewSizeFake.offsetHeight );
        objPreview.style.display = 'none';
    }
}
function onPreviewLoad(sender){
    autoSizePreview( sender, sender.offsetWidth, sender.offsetHeight );
}
function autoSizePreview( objPre, originalWidth, originalHeight ){
    var zoomParam = clacImgZoomParam( 300, 300, originalWidth, originalHeight );
    objPre.style.width = zoomParam.width + 'px';
    objPre.style.height = zoomParam.height + 'px';
    objPre.style.marginTop = zoomParam.top + 'px';
    objPre.style.marginLeft = zoomParam.left + 'px';
}
function clacImgZoomParam( maxWidth, maxHeight, width, height ){
    var param = { width, height:height, top:0, left:0 };
    if( width>maxWidth || height>maxHeight ){
        rateWidth = width / maxWidth;
        rateHeight = height / maxHeight;
        if( rateWidth > rateHeight ){
            param.width = maxWidth;
            param.height = height / rateWidth;
        }else{
            param.width = width / rateHeight;
            param.height = maxHeight;
        }
    }
    param.left = (maxWidth - param.width) / 2;
    param.top = (maxHeight - param.height) / 2;
    return param;
}
</script>
</head>
<body>
    <div id="preview_wrapper">
        <div id="preview_fake">
            <img id="preview" onload="onPreviewLoad(this)"/>
        </div>
    </div>
    <br/>
    <input id="upload_img" type="file" onchange="onUploadImgChange(this)"/>
    <br/>
    <img id="preview_size_fake"/>
</body>
</html>

免责声明:文章转载自《【转】inputfile的本地图片预览+等比例缩放兼容IE8、火狐》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇精选 5 个漂亮的 CSS3 图片滑过特效ceph ---(ceph简介)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PSR

PSR是PHP Standards Recommendation的简称,这个是php-fig组织制定的一套规范。 PSR-1 PHP标签:PHP代码必须放在<?php ?>标签或<?= ?>标签中。 编码:PHP文件必须使用无BOM的UTF-8编码。 副作用:一个PHP文件可以定义符号(比如类、函数、常量等),或者执行只有唯一副...

mybatis父查询值嵌套传递/column传入多个参数值197

mybatis中collection的column传入多个参数值(使用父查询的映射值) property description column 数据库的列名或者列标签别名。与传递给resultSet.getString(columnName)的参数名称相同。注意: 在处理组合键时,您可以使用column=“{prop1=col1,prop2=co...

微信app支付,完整流程,完整代码 (转)

微信app支付流程 需要的配置参数 private function wechat($body,$indent_id,$cou,$user_id,$total_fee,$ip,$domain,$nonce_str){ //微信配置信息和初始逻辑 $appid= WxPayConfig::APPID; //appid (微信开放平台的应用appid) $bo...

web_reg_save_param_ex简介

Save Offset 设置关联的内容偏移量,从第几位开始进行关联操作。回到最开始的例子,我们抓取的是You have successfully installed XAMPP on this system!,如果需要获得successfully installed XAMPP on this system!这个字符串,则不用改变左边界,只需要设置Sav...

angular 路由跳转以及传参的几种方式

通过域名跳转的方式获取参数(http://localhost:4200/second/110?productId=1&title=moon) 这种方式配置路由,其中:id是必需的参数,其它的是可配的,写在?后面: { path: 'second/:id', component: SecondComponent }, 通过ts代码跳转: this...

layui从子iframe打开父iframe的tab选项卡

数据表格字段: {field: 'novelId', title: '小说ID',100,templet: '<div><a href="javascript:;" _href="/contentManage/content-chapters.html" novelName="{{d.name}}" onclick="showChapte...