Javascript 在Ajax调用之前,是否可以读取Microsoft Excel文件的内容?

Javascript 在Ajax调用之前,是否可以读取Microsoft Excel文件的内容?,javascript,jquery,excel,ajax,Javascript,Jquery,Excel,Ajax,我正在尝试在上载之前验证文件。我正在验证文件类型(即仅允许.xls、.xlsx),但我需要确保他们正在提交特定的Microsoft Excel文件。这是至关重要的,因为我正在将数据上传到一个表中。如果提交的Excel文件是错误的,则表中的数据将不正确。文件名不能用来验证它,因为我不确定最终用户是否会保持相同的命名约定 我能知道第一个柱子的名字吗 这是迄今为止我掌握的代码 $(document).ready(function () { // label when file is sel

我正在尝试在上载之前验证文件。我正在验证文件类型(即仅允许.xls、.xlsx),但我需要确保他们正在提交特定的Microsoft Excel文件。这是至关重要的,因为我正在将数据上传到一个表中。如果提交的Excel文件是错误的,则表中的数据将不正确。文件名不能用来验证它,因为我不确定最终用户是否会保持相同的命名约定

我能知道第一个柱子的名字吗

这是迄今为止我掌握的代码

$(document).ready(function () { 

    // label when file is selected
    $('input[type="file"]').change(function(e){
        var fileName = e.target.files[0].name;
        $('.custom-file-label').html(fileName);
    });

    $("#btnUpload").click(function (event) {
    
    // validate filetype
    var fileID = document.getElementById('uploadEmpFile');
        fileName = fileID.value;
        fileType = fileName.substring(fileName.lastIndexOf('.') + 1);

        console.log(fileType);

        if (fileType == "" || fileType == null) {
            alert('You never selected a file !');

        }
        else if (!(fileType == 'xlsx' || fileType == 'xls' || fileType == 'xlsm')) {
            alert('As it turns out, this particular file type is not compatible.');

        }
        else {
            //stop submit the form, we will post it manually.
            event.preventDefault();

            // Get form
            var form = $('#fileUploadForm')[0];

            // Create an FormData object 
            var data = new FormData(form);

            // If you want to add an extra field for the FormData
            data.append("CustomField", "This is some extra data, testing");

            // disabled the submit button
            $("#btnUpload").prop("disabled", true);

            $('#loader').removeClass('d-none');
            $.ajax({
                type: "POST",
                enctype: 'multipart/form-data',
                url: "http://localhost:61591/api/Inservice/OfficeStaffUpload",
                data: data,
                processData: false,
                contentType: false,
                cache: false,
                timeout: 100000,
                success: function (data) {
                    var responseData = JSON.parse(data);
                    $("#loader").addClass("d-none");

                    if (responseData.result == "Success") {
                        alert('The Employee Roster has successfully been uploaded.');
                        window.location = "hrAdmin.html";
                        $("#btnUpload").prop("disabled", false);
                    }
                },
                error: function (e) {
                    $("#result").text(e.responseText);
                    console.log("ERROR : ", e);
                    $("#btnUpload").prop("disabled", false);

                }
            });
        }
    });
});

你最好在服务器上这样做,你不能信任任何客户端验证。是的。这应该在插入之前完成,但在调用之后。因此,在服务器端,它需要验证。我会调查的。非常感谢。