asp.net - 如果文件已存在,jQuery 拦截并阻止表单提交

标签 asp.net asp.net-mvc asp.net-mvc-3 jquery

我使用 MVC3,并且有一个带有文件上传的表单。如果该文件已存在于服务器上,我想提示用户确认他们想要覆盖该文件。我在表单提交上添加了一个 jQuery 方法来处理我在线阅读的内容,但似乎该帖子在显示确认对话框之前已被初始化。

如果我在表单提交函数顶部调用 e.preventDefault() ,它会停止表单,但我不知道如何重新调用该操作。这是我所拥有的:

表格

@using (Html.BeginForm("Upload", "Management", FormMethod.Post, new {id = "formUpload", enctype = "multipart/form-data"})) {

    <div class="editor-label">Pricing Model Workbook</div>
    <div class="editor-field">
        <input type="file" name="file" id="file" size="50" />
        @Html.ValidationMessageFor(file => file.FileName)
    </div>
    <div><input type="submit" name="upload" id="upload" value="Upload" /></div>
}

jQuery

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $('#formUpload').submit(function (e) {
            var filePath = $('#file').val();
            $.getJSON('@Url.Action("CheckIfFileExists")', { path: filePath },
                function (exists) {
                    if (exists) {
                        var cancel = confirm('File "' + filePath + '" has already been uploaded. Overwrite?');
                        if (cancel) {
                            e.preventDefault();
                            return false;
                        }
                    }

                    return true;
                }
            );
        });
    });
</script>

所以我的问题是,我做错了什么?另外,作为奖励,如果客户端验证捕获错误,是否有任何方法可以防止弹出此错误?

任何帮助将不胜感激!

更新 我最终做到了这一点,这对我想做的事情很有用。

    <script type="text/javascript" language="javascript">
    $(document).ready(function () {
        var fileInvalid = false;

        // check if file exists when user selects a new file
        $('#file').change(function () {
            var filePath = $('#file').val();
            $.getJSON('@Url.Action("CheckIfFileExists")', { path: filePath },
                function (exists) {
                    if (exists) {
                        var overwrite = confirm('Warning :: File "' + filePath + '" has already been uploaded.'
                                                + 'The existing data will be overwritten on submission. Continue?');
                        if (!overwrite) {
                            $('#file').replaceWith($('#file').clone(true));
                        }
                    }
                }
            );
        });
    });
</script>

最佳答案

问题是您的 Ajax 请求只有在提交处理程序完成后才可能完成;因此,该帖子将继续,您将无法取消它。听起来你需要的是一扇门;基本上,除非您将标志设置为允许,否则您无法提交。像这样:

 var fileInvalid = true;
 $('#file').change(function()
 {
     $.getJSON('@Url.Action("CheckIfFileExists")', { path: filePath },
            function (exists) {
                if (exists) {
                    var cancel = confirm('File "' + filePath + '" has already been uploaded. Overwrite?');
                    if (cancel) {
                        fileInvalid = false;
                    }
                }
                else
                {
                    fileInvalid = false;
                }
            }
        );
 });

 $('#formUpload').submit(function(e)
 {
     if(fileInvalid)
        e.preventDefault();
 });

关于asp.net - 如果文件已存在,jQuery 拦截并阻止表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7338360/

相关文章:

asp.net-mvc - @ Html.EditorFor()呈现DropDownList而不是CheckBox

asp.net-mvc-3 - MSBuild IIS 问题

asp.net - 如何使用 asp.net 从字符串中删除 HTML 标签(粗体、粗体、字体名称等)

asp.net - OutputCache.VaryByHeader 未在响应中生成 Vary header

c# - 从文本文件中读取并将其写入 XML

asp.net-mvc - 使文本框仅在单击同一 View 中的“编辑”按钮后才可写入

c# - Dropbox 整合

c# - 带有 viewModel 的嵌套局部 View

c# - 获取 Web 应用程序程序集名称,无论当前正在执行的程序集如何

c# - MVC3 复选框值不会清除