user1624306
user1624306

Reputation: 311

upload file using jquery in mvc razor

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

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

Upvotes: 0

Views: 1522

Answers (1)

Balton
Balton

Reputation: 31

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>

Upvotes: 1

Related Questions