0

I am new to MVC 3 Razor. How to upload file using Jquery.My code below mentioned

@using (Html.BeginForm())
    {
        <input type='file' name='file' id='file' />
        <input type="Button" value="upload" />
    }

few restriction is below mentioned

  • i can not define my action and controller within html.BeginForm(...)
  • i can not use type="submit" of above upload button.

Now i hope you are clear when i click upload button jquery function will be call and from there my action should be call and in controller i want to implement my logic with uploaded file.

please let me know how to implement this.????any sample demo??

1 Answer 1

1

It's easy

@using (Html.BeginForm())
    {
        <input type='file' name='file' id='file' />
        <input type="Button" value="upload" onclick="upload()" />
    }

<script>
    function upload() {
        $.ajax({
            type: "POST",
            url: '@Url.Action("Upload")',
            dataType: "multipart/form-data",
            data: $('#file'),
            cache: false
        });
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.