修改上传文件按钮的风格

一段 HTML 代码,目标是显示 bootstrap 风络的上传按钮(不带路径显示),且点击后无需再点 submit 按钮,直接发 post 请求。

[](https://img-blog.csdnimg.cn/20190119165910888.png?x-oss-

process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpZXlhbjA4MTE=,size_16,color_FFFFFF,t_70)

<!DOCTYPE html>
<html>
    <head>
        <link href="bootstrap.css" rel="stylesheet" media="screen">
        <style>
            .fileinput-button {
                position: relative;
                display: inline-block;
                overflow: hidden;
            }
            .fileinput-button input{
                position:absolute;
                right: 0px;
                top: 0px;
                opacity: 0;
                -ms-filter: 'alpha(opacity=0)';
                font-size: 200px;
            }
        </style>
    </head>

    <body>
        <div class="container">
            <nav class="navbar navbar-default navbar-fixed-center" role="navigation" style="padding:5px;">
                <form action="/upload" method="post" enctype="multipart/form-data">
                    <span class="btn btn-primary fileinput-button">
                        <span>Upload</span>
                        <input type="file" accept=".xls, .xlsx, .csv" id="inputBtn" value="上传"/>
                    </span>
                </form>
            </div>
            </nav>
        </div>

        <script type="text/javascript" src="jquery.js" charset="UTF-8"></script>
        <script type="text/javascript" src="bootstrap.min.js"></script>

        <script>
            $("#inputBtn").change(function(event) {
                    file = event.target.files[0];
                    var formData = new FormData();
                    formData.append("fileUpload" , file);     
                    $.ajax({
                        url : "upload",
                        datatype : "json",
                        type : "post",
                        data: formData ,
                        processData : false,
                        contentType : false,
                        success:function(result){
                            alert(result)
                        }
                });
            })
        </script>

    </body>
</html>

注意需要把支持 bootstrap 的几个 css 的 js 文件复制到同一目录下,同时提供 upload 程序接收文件,并反馈显示信息。