Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6532519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:00:48+00:00 2026-05-25T10:00:48+00:00

I using MVC3 and I have a form with a file upload. If the

  • 0

I using MVC3 and I have a form with a file upload. If the file already exists on the server, I want to prompt the user to confirm that they want to overwrite the file. I have added a jQuery method on the form submit to handle this from what I’ve read online, but it seems like the post is being intialized before it can display the confirm dialog..

If I call e.preventDefault() at the top of my form submit function it stops the form, but then I don’t know how to reinvoke the action. Here is what I have:

The Form

@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>
}

The 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>

So my question is, what am I doing wrong? Also, as a bonus, is there any way to prevent this from popping up if the clientside validation catches an error?

Any help would be greatly appreciated!!!

UPDATE
I ended up doing this and this works for what I was trying to do.

    <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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:00:48+00:00Added an answer on May 25, 2026 at 10:00 am

    The problem is that your Ajax request doesnt potentially complete until after the submit handler is finished; ergo, the post continues and you lose the ability to cancel it. It sounds like what you need is a gate; basically, until you set the flag as allowable, you can’t submit. Something like so:

     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();
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MVC3 VB.NET Application. I have multiple file upload boxes on a form in my
I'm using MVC3 with unobtrusive validation. I have a field that the user is
I am using MVC3 and I want to have LogIn form and Register form
Using MVC3, C#, and the Razor View Engine: I have a form that has
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I have an ASP.NET MVC3 app that features a form with a nested-table input
i have a view that shows the user a form and the user should
Using MVC3 I have found that setting a SelectList's selected value correctly renders the
Have an ASP.NET MVC3 app with model validation using FluentValidation . User enters some
I have a form on a webpage that I submit using the jquery form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.