单击按钮文件上传

 <button  id="uploadExcel" onclick="uploadExcel()"> 上传文件</button>
<script>
        /*导入Excel*/
        function uploadExcel() {
            /*#load:为按钮,点击后生成一个隐藏的input file标签*/
            $('#uploadExcel').after('<input type="file" id="upload_xls" name="file" style="display:none" onchange ="uploadFile()">');
            document.getElementById("upload_xls").click();
        }

        function uploadFile() {
            //选择文件
            var filePath = $("#upload_xls").val();
            //设置上传文件类型
            if (filePath.indexOf(".xls") != -1 || filePath.indexOf(".xlsx") != -1) {
                var form = new FormData();
                form.append('file', $('#upload_xls')[0].files[0]);
                //上传文件
                $.ajax({
                    url: 'uploadFile.do',
                    type: 'post',
                    data: form,
                    cache: false,
                    //阻止Ajax将参数转成成String类型的键值对,需要设置processData属性为false,
                    processData: false,
                    //form的enctype="multipart/form-data"才能上传文件。Ajax的默认编码方式是application/x-www-form-urlencoded
                    contentType: false,

                    success: function (r) {
                        if (r.state == 200) {
                            alert(r.msg);
                        }
                        if (r.state == 300) {//上传文件有错误信息
                            var filename = r.data;
                            if (confirm(r.msg)) {
                                location.href = 'download.do?filename=' + filename;
                            }
                        }
                    }
                });
            } else {
                alert("请选择正确的文件格式!");
                return false;
            }
        }
    </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值