Javascript 当文件上载控件上没有任何文件时显示消息单击按钮

Javascript 当文件上载控件上没有任何文件时显示消息单击按钮,javascript,jquery,html,file-upload,Javascript,Jquery,Html,File Upload,我的页面上有一个文件上载控件,我想限制只接受.csv文件,并在试图上载任何其他文件时抛出消息。此外,当没有选择任何文件并且单击了上载按钮时,它应该抛出消息 Html- <div class="panel panel-primary"> <div class="panel-heading">00</div> <div class="panel-body"> <div class="field_row">

我的页面上有一个文件上载控件,我想限制只接受.csv文件,并在试图上载任何其他文件时抛出消息。此外,当没有选择任何文件并且单击了上载按钮时,它应该抛出消息

Html-

<div class="panel panel-primary">
    <div class="panel-heading">00</div>
    <div class="panel-body">
        <div class="field_row">
            <input type="file" accept=".csv" id="fileUpload" name="fileUpload1" />
        </div>
        <br />
        <div class="field_row">
            <div class="col-lg-12">
                <div class="form-group">
                    <center><div class="col-lg-3"><div class="input-append" id="filterDev" style="visibility: hidden">
                        <input name="search" id="inputFilter" placeholder="Enter ID to filter" />
                        <input type="button" value="Filter" id="filter" class="btn btn-primary" />
                    </div></div></center>
                    <div class="col-lg-6"></div>
                    <input type="button" id="upload" value="Upload" class="btn btn-primary" />
                    <input type="button" id="cancel" value="Cancel/Save" style="visibility: hidden" class="btn btn-primary" />
                    <input type="button" id="process" value="Process" style="visibility: hidden" class="btn btn-primary" />
                </div>
            </div>

            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default" style="align-self: center">
                        <div class="panel-body" style=" max-height:600px; min-height: 400px; overflow-y: scroll;">

                            <div class="row">
                                <div class="col-sm-12" id="dvCSV">
                                    <table id="my-table">
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <p id="download" style="color: cornflowerblue; visibility: hidden"><strong>Please click the below links to download the processed file..</strong></p>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="col-sm-3">
                    <p id="File1" style="color: cornflowerblue; text-decoration: underline; visibility: hidden"><a href="Files/CommitteeMembers.xlsx">File1 Download</a></p>
                </div>
                <div class="col-sm-3">
                    <p id="File2" style="color: cornflowerblue; text-decoration: underline; visibility: hidden"><a href="Files/GSD_Offering_Completion.csv">File2 Download</a></p>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

<script type="text/javascript">

    $("#cancel").on("click", function () {
        $('input:checked').each(function () {
            $(this).closest("tr").remove();
        });
    });


    $(function () {
        $("#process").bind("click", function () {

            document.getElementById("File1").style.visibility = "visible";
            document.getElementById("File2").style.visibility = "visible";
            document.getElementById("download").style.visibility = "visible";

        });

    });

    $("#filter").click(function () {
        ids = $("#inputFilter").val();
        if (ids != "") {
            idsArray = ids.split(",");
            $("#my-table tr:gt(0)").hide();
            $.each(idsArray, function (i, v) {
                $("#my-table tr[data-id=" + v + "]").show();
            })
        } else {
            $("#my-table tr").show();
        }
    });


    /*  $("#File1").click(function () {
          window.location = "Files/CommitteeMembers.xlsx";
      });
      $("#File1").click(function () {
          window.location = "Files/GSD_Offering_Completion.xlsx";
      });*/

</script>

00

请单击以下链接下载处理后的文件。

$(“#取消”)。在(“单击”上,函数(){ $('input:checked')。每个(函数(){ $(this).tr.remove(); }); }); $(函数(){ $(“#进程”).bind(“单击”,函数(){ document.getElementById(“File1”).style.visibility=“可见”; document.getElementById(“File2”).style.visibility=“可见”; document.getElementById(“下载”).style.visibility=“可见”; }); }); $(“#过滤器”)。单击(函数(){ ids=$(“#inputFilter”).val(); 如果(ID!=“”){ idsArray=ids.split(“,”); $(“#我的表tr:gt(0)”).hide(); $.each(数组、函数(i、v){ $(“#我的表tr[data id=“+v+”])。show(); }) }否则{ $(“#我的桌子tr”).show(); } }); /*$(“#文件1”)。单击(函数(){ window.location=“Files/committeembers.xlsx”; }); $(“#文件1”)。单击(函数(){ window.location=“Files/GSD\u Offering\u Completion.xlsx”; });*/
Javascript-

$(function () {
    $("#upload").bind("click", function () {
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;

        if (regex.test($("#fileUpload").val().toLowerCase())) {
            if (typeof (FileReader) != "undefined") {
                var reader = new FileReader();
                reader.onload = function (e) {
                    //var table = $("<table id='name'/>");
                    var lines = e.target.result.split("\n");
                    //alert(lines);
                    var result = [];


                    var headers = lines[0].split(",");
                    for (var i = 1; i < headers.length; i++) {
                        var header = headers[i];
                        header = header.replace(/(\r\n|\n|\r)/gm, "");
                        headers[i] = header;
                    }



                    for (var i = 1; i < lines.length; i++) {
                        var obj = {};
                        var currentline = lines[i].split(",");
                        //alert(currentline);

                        for (var j = 0; j < headers.length; j++) {
                            obj[headers[j]] = currentline[j];
                            //alert(obj[headers[j]]);
                        }
                        result.push(obj);
                        //alert(JSON.stringify(result));
                    }

                    //alert(result[0].NAME + ' '+ result[0].ADDRESS+" "+result[0].CITY);
                    populateTable(result);
                    document.getElementById("cancel").style.visibility = "visible";
                    document.getElementById("process").style.visibility = "visible";
                    document.getElementById("filterDev").style.visibility = "visible";
                }
                reader.readAsText($("#fileUpload")[0].files[0]);
            }

        }

    });
});

$(function () {
    $("#process").bind("click", function () {

        document.getElementById("File1").style.visibility = "visible";
        document.getElementById("File2").style.visibility = "visible";

    });

});

function populateTable(finalObject) {

    var obj = finalObject;
    var table = $("<table id='my-table' />");
    table[0].border = "1";
    var columns = Object.keys(obj[0]);
    columns.unshift('');
    //alert(columns);
    var columnCount = columns.length;
    var row = $(table[0].insertRow(-1));


    for (var i = 0; i < columnCount; i++) {
        var headerCell = $("<th />");
        headerCell.html([columns[i]]);
        row.append(headerCell);
    }

    $.each(obj, function (i, obj) {

        row = '<tr data-id="' + obj.ID + '"><td><input type="checkbox"/></td><


    var dvTable = $("#dvCSV");
    dvTable.html("");
    dvTable.append(table);

} 
$(函数(){
$(“#上载”).bind(“单击”),函数(){
var regex=/^([a-zA-Z0-9\s\\\.\-:])+(.csv|.txt)$/;
if(regex.test($(“#fileUpload”).val().toLowerCase()){
if(typeof(FileReader)!=“未定义”){
var reader=new FileReader();
reader.onload=函数(e){
//变量表=$(“”);
变量行=e.target.result.split(“\n”);
//警戒线;
var结果=[];
var headers=行[0]。拆分(“”,“”);
对于(变量i=1;i
如何实现这一点