Ajax方法在按钮单击中工作两次

时间:2016-02-10 10:59:07

标签: javascript jquery ajax asp.net-ajax

上传按钮上的文件上传功能单击。功能

$("#fuPDFAdd").change(function () {})

文件上传更改工作两次,点击' btnUploadAdd'按钮。 怎么可以避免这个

<div id="divUploadFileAdd">
    <form enctype="multipart/form-data" id="frmUplaodFileAdd">
        @Html.AntiForgeryToken()
        <input id="fuPDFAdd" name="file" type="file" style = "display:none;"/> 
        <button class="" id="btnUploadAdd" type="button" onclick="test()">Upload</button>
        <label id="txtuploadedMsgAdd"> </label>
    </form>
</div>
$(document).ready(function () {
    $("#fuPDFAdd").change(function () {
        console.log("tst1");
        var file = this.files[0];
        fileName = file.name;
        size = file.size;
        type = file.type;
        if (type.toLowerCase() == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { //I just want pdf files and only want to show
            var formData = new FormData($('#frmUplaodFileAdd')[0]);
            $.ajax({
                url: "UploadFile",  //Server script to process data
                type: 'POST',
                async: false,
                xhr: function () {  // Custom XMLHttpRequest
                    var myXhr = $.ajaxSettings.xhr();
                    if (myXhr.upload) { // Check if upload property exists
                        myXhr.upload.addEventListener('progress',
                        progressHandlingFunction, false); // For handling the progress of the upload
                    }
                    return myXhr;
                },
                data: formData,
                //Options to tell jQuery not to process data or worry about content-type.
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    grdStaffAddition.PerformCallback({ transStatus: "New" });
                    ShowClientToastr('False', 'False', 'toast-bottom-right', 'True', 'success', 'Template migration completed' + data.result, 'CAM - Contract Staff');
                }
            });
        }
        else {
            ShowClientToastr('False', 'False', 'toast-bottom-right', 'True', 'error', 'Please select xls/xlsx file.', 'CAM - Contract Staff');
        }
    });
});

function test() {   
    $("#fuPDFAdd").click();
}

1 个答案:

答案 0 :(得分:0)

这样的东西会起作用,我确实将文件上传代码包装到一个单独的函数中,并让它从你的事件监听器中调用这个新函数。

function uploadTheFile() {
    console.log("tst1");
    var file = this.files[0];
    fileName = file.name;
    size = file.size;
    type = file.type;
    if (type.toLowerCase() == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { //I just want pdf files and only want to show
        var formData = new FormData($('#frmUplaodFileAdd')[0]);
        $.ajax({
            url: "UploadFile", //Server script to process data
            type: 'POST',
            async: false,
            xhr: function() { // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) { // Check if upload property exists
                    myXhr.upload.addEventListener('progress',
                        progressHandlingFunction, false); // For handling the progress of the upload
                }
                return myXhr;
            },
            data: formData,
            //Options to tell jQuery not to process data or worry about content-type.
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
                grdStaffAddition.PerformCallback({
                    transStatus: "New"
                });
                ShowClientToastr('False', 'False', 'toast-bottom-right', 'True', 'success', 'Template migration completed' + data.result, 'CAM - Contract Staff');

            }
        });
    } else {
        ShowClientToastr('False', 'False', 'toast-bottom-right', 'True', 'error', 'Please select xls/xlsx file.', 'CAM - Contract Staff');

    }
}
$("#fuPDFAdd").change(function() {
    uploadTheFile();
});

function test() {
    uploadTheFile();
}