基于Html5的文件上传

一个文件上传的小demo,支持Chorm,firefox等主流浏览器,不支持IE。通过ajax代码实现多文件异步上传,带有进度条,不使用三方控件,方便修改而不用纠结于三方控件的配置。效果图:

这里写图片描述

页面代码:

<body>
    <div>
        Welcome to home page.
        <input type="button" value="open" id="openMode" name="openMode" />

    </div>

@using (Html.BeginForm("", "", FormMethod.Post, new { id = "excelForm", enctype = "multipart/form-data" }))
 {

    <div id="main_div" class="block">
        <div class="divBlock">
            <div style="width: 100%; margin: -1px auto; height: 25px; background-color: rgb(21,112,166); color: white; font-size: 15px; -moz-border-radius: 20px; -webkit-border-radius: 20px; border-radius: 20px; ">

                <div style="width: 100%;margin:-1px -1px -1px -1px; height: 40px; position: absolute; top: 13px; height: 20px; background-color: rgb(21,112,166);">
                    <div style="width: 80%; padding-top: -5px; padding-left: 10px;">
                        上传文件
                    </div>
                </div>

            </div>
                <div style="float:none;"></div>
            <div class="buttonDiv">
                <a href="#" id="selectFile" name="selectFile" class="uploadButton">
                    select file
                    <input type="file" name="uploadFile" id="uploadFile" class="file" value="浏览" />
                </a>
               @* <input type="button" value="click" id="test" name="test"/>*@
                <input id="fileName" name="fileName" type="hidden" />
            </div>


            <div class="divFileList">
                <table class="table">

                    <tbody id="tbodyFile">

                    </tbody>
                </table>

            </div>

        </div>
       </div>
 }
</body>

页面样式:

<style type="text/css">
    .block {
        width:100%;
        height:100%;
        display:none;
        position:absolute;
        top:0px;
        left:0px;
        opacity:0.8;
        background-color:#dedbdb ;
    }

    .divBlock {
        width: 400px;
        height: 400px;
        position: absolute;
        opacity: 0.8;
        top: 50px;
        left: 350px;
        border: dotted 1px;
        background-color: #496F75;
        -moz-border-radius: 20px;
        -webkit-border-radius: 20px;
        border-radius: 20px;
    }

    .divMain {
        width: 650px;
    }

    td {
        height: 30px;
    }

    h1, h2, h3 {
        font-size: 12px;
        margin: 0;
        padding: 0;
    }

    .table {
        width: 100%;
        margin: auto auto;
    }
        .table tbody {
            width: 100%;
        }

        .table th {
            background-repeat: repeat-x;
            height: 30px;
        }
        .table tr
        {
            width:100%;
            height:60px;
        }

        .table td, .table th {

            text-align: center;
            margin: auto auto;
            height:60px;
        }

        .table tr.alter {
            background-color: #f5fafe;
        } 

    .excelbutton {
        filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#ffffffff', EndColorStr='#ffeeddaa');
        font: bold 90% 'trebuchet ms', helvetica, sans-serif;
        display: inline-table;
        width: 80px;
        height: 20px;
        position: relative;
        overflow: hidden;
        text-align: center;
        padding-top: 0px;
        font-size: 13px;
        padding-top: 4px;
        text-decoration:none;
    }

        .excelbutton:hover {
            border: 1px solid;
            border-color: #c63 #930 #930 #c63;
            background-color: #050;
        }

    .file {
        position: absolute;
        right: 0;
        top: 0;
        font-size: 100px;
        opacity: 0;
        filter: alpha(opacity=0);
        height: 20px;
        background-color: rgb(21,112,166);
        cursor: pointer;
    }

    .buttonDiv {
        width: 70%;
        margin: 15px 10px;
        height: 10px;
        position:relative;
    }
    .uploadButton {
        display: inline-block;
        position: relative;
        overflow: hidden;
        text-align: center;
        color: #050;
        background-color:#d3d0d0;
        font-size: 11px;
        width: 80px;
        height: 15px;
        padding-top: 1px;
        cursor: pointer;
        text-decoration: none;
        margin-top: 3px;
    }
    .divFileList
    {
        width:98%;
        height:320px;
        border:double 2px #e9dede;
        overflow:auto;
        margin:auto auto;
    }
    .divSignalFile
    {
        width:99%;
        height:60px;
        margin-top:2px;
        margin-left:auto;
        text-align:left;
        border:dotted 1px #dedbdb;
        padding-left:2px;
        padding-top:2px;
    }
    .divSignalFileleft
    {
          width:97%;
          height:60px;
          float:left;
          font-size:12px;
          padding-top:2px;
          padding-left:5px;
    }
    .divSignalFileright
    {
        width:50px;
        height:100px;
        float:left;
        text-align:center;
        padding-top:20px;
        margin-left:10px;
    }    
</style>

JS代码:

<script type="text/javascript">


    $(function () {


        $("#excelForm").submit(function () {

            $(this).ajaxSubmit(optionss);
            //$(this).resetForm();  提交后重置表单
            return false;

        });


        $("#uploadFile").change(function () {
            var mydate = new Date();
            var mintues = mydate.getMinutes(); //获取当前分钟数(0-59)
            var seconds = mydate.getSeconds(); //获取当前秒数(0-59)

            var extention = mintues.toString() + seconds.toString();
            var file = document.getElementById('uploadFile').files[0];

            if (file) {
                var test = "";
                test += "<tr name='" + file.name + extention + "'>";
                test += "         <td style='height:60px;'>";
                test += " <form id='" + file.name + "' method='post' enctype='multipart/form-data'>";
                test += "            <div class='divSignalFile'>     ";
                test += "               <div class='divSignalFileleft'>    ";
                test += "         <div style='width:100%;height:20px;'>    ";
                test += "            <div name='fileTitle'  style='width:100%;padding-top:3px;' >    ";

                test += "                </div>       ";
                test += "        </div>     ";
                test += "       <div style ='width:100%;height:15px;text-align:left;border:solid 1px #8e8c8c;margin-top:2px;'>    ";
                test += "                          <div name='progressBar' data-value='10' style='width:0px;height:14px;margin-top:1px;background-color:#B1D632;'>  ";

                test += "                          </div>      ";
                test += "                      </div>    ";
                test += "                      <div id='progressInfo' style='width:100%;height:20px;padding-top:3px;'>   ";
                test += "                           <div name='current' style='width:60px; float:left;text-align:left;'> ";

                test += "                           </div>      ";
                test += "                          <div name='total' style='width:80px;float:left;text-align:left'></div>    ";
                test += "                          <div name='percent' style='width:40px;float:right;text-align:right'>      ";

                test += "                          </div>         ";
                test += "                          <div style='float:none;'></div>    ";
                test += "                      </div>   ";
                test += "                  </div>   ";

                test += "              <div style='float:none'>     ";

                test += "              </div>      ";
                test += "          </div>    ";
                test += "  </form> ";
                test += "      </td>   ";
                test += "  </tr>   ";

                $("#tbodyFile").append(test);

                sumbit("excelForm", extention);
            }

            //$("#excelForm").submit();
        });

        $("#openMode").bind('click', function () {

            $("#main_div").show();
        });

    });



    function sumbit(form,extention)
    {
        var that = $("#" + form);

        var file = document.getElementById('uploadFile').files[0];
        var optionss = {
            //dataType:"json", 
            //data: $("#excelForm").serializeArray(),
            type: 'post',
            url: "/FileUpload/Upload/upfile",
            beforeSubmit: showRequest,
            success: showResponse,

            beforeSend: function () {
                //$("#current").html("Scan files...");
                //var file = document.getElementById('uploadFile').files[0];
                var fileSize = 0;
                if (file) {
                    if (file.size > 1024 * 1024)
                        fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
                    else
                        fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';

                    if (file.name.length > 50) {
                        $("#tbodyFile").find("tr[name='" + file.name +extention+ "']").find("div[name='fileTitle']").html(file.name.substring(0, 25) + "...");
                    }
                    else {
                        $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='fileTitle']").html(file.name);
                    }

                }
                else {
                    $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='fileTitle']").html("Please select file");
                    return false;
                }
                // 0KB/100M     50%     80kb/s 
                //total

                $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='current']").html("0KB");
                $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='total']").html("/" + fileSize);
                $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='percent']").html("0%");
            },
            uploadProgress: function (event, position, total, percentComplete) {
                var fileSize = 0;
                if (position > 1024 * 1024)
                    fileSize = (Math.round(position * 100 / (1024 * 1024)) / 100).toString() + 'MB';
                else
                    fileSize = (Math.round(position * 100 / 1024) / 100).toString() + 'KB';

                var fileTotalSize = 0;
                if (total > 1024 * 1024)
                    fileTotalSize = (Math.round(total * 100 / (1024 * 1024)) / 100).toString() + 'MB';
                else
                    fileTotalSize = (Math.round(total * 100 / 1024) / 100).toString() + 'KB';
                $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='total']").html("/" + fileTotalSize);


                $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='progressBar']").css("width", percentComplete.toString() + "%");
                $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='current']").html(fileSize);
                $("#tbodyFile").find("tr[name='" + file.name + extention + "']").find("div[name='percent']").html(percentComplete.toString() + "%");

            },

            clearForm: false
        };





        that.ajaxSubmit(optionss);
    }



        function showResponse(data) {

        }

        function showRequest(data) {
        }

</script>

需要引用的js:

jquery-1.10.2.js
jquery-1.10.2.min.js
jquery-ui-1.10.3.js
jquery-form.js

webconfig配置(当前设置最大上传文件4G):
< system.web>节点

 <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime maxRequestLength="40960000"
    executionTimeout="600"
                 appRequestQueueLimit="100"
    />
  </system.web>

< system.webServer>节点:

<system.webServer>
    <security>
      <requestFiltering >
        <requestLimits maxAllowedContentLength="4096000000" ></requestLimits>
      </requestFiltering>
    </security>
  </system.webServer>

资源文件:
多文件上传demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值