当前位置:编程学习 > JS >>

js 实现 < input type="file" / > 文件上传

在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但是它长得又丑、浏览的字样不能换,我们一般会用让,<input type="file" />隐藏,点其他的标签(图片

等)来时实现选择文件上传功能。

看代码:

[html]
<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script> 
    <style type="text/css"> 
        ._box 
        { 
            width: 119px; 
            height: 37px; 
            background-color: #53AD3F; 
            background-image: url(images/bg.png); 
            background-repeat: no-repeat; 
            background-position: 0 0; 
            background-attachment: scroll; 
            line-height: 37px; 
            text-align: center; 
            color: white; 
            cursor: pointer; 
        } 
 
        .none 
        { 
            width: 0px; 
            height: 0px; 
            display: none; 
        } 
    </style> 
    <title>js 实现 input file 文件上传 /></title> 
</head> 
<body> 
    <form id="form1" runat="server" method="post" enctype="multipart/form-data"> 
        <div> 
            <div class="_box">选择图片</div> 
        </div> 
        <div class="none"> 
            <input type="file" name="_f" id="_f" /> 
        </div> 
    </form> 
</body> 
</html> 
<script type="text/javascript"> 
    jQuery(function () { 
        $("._box").click(function () { 
            $("#_f").click(); 
        }); 
    }); 
</script> 
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script>
    <style type="text/css">
        ._box
        {
            width: 119px;
            height: 37px;
            background-color: #53AD3F;
            background-image: url(images/bg.png);
            background-repeat: no-repeat;
            background-position: 0 0;
            background-attachment: scroll;
            line-height: 37px;
            text-align: center;
            color: white;
            cursor: pointer;
        }

        .none
        {
            width: 0px;
            height: 0px;
            display: none;
        }
    </style>
    <title>js 实现 input file 文件上传 /></title>
</head>
<body>
    <form id="form1" runat="server" method="post" enctype="multipart/form-data">
        <div>
            <div class="_box">选择图片</div>
        </div>
        <div class="none">
            <input type="file" name="_f" id="_f" />
        </div>
    </form>
</body>
</html>
<script type="text/javascript">
    jQuery(function () {
        $("._box").click(function () {
            $("#_f").click();
        });
    });
</script>
 

但是在火狐和一些高版本的浏览器中后台可以获取到要上传的文件,一些低版本的浏览器压根就获取不到Request.Files

 

查阅资料,有说改成这样的:

 

[html]
<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <script src="js/jquery/jquery-1.8.2.min.js" type=&

补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,