File Upload in ASP.NET MVC3

If you are a web developer you often need to upload file on the web server or database. In today’s post I am going explain how we can upload file in ASP.NET MVC 3 with razor syntax.

So, first thing we need to create a view that will contain the file upload control. I am going to use an existing asp.net mvc template for this demo. So I have just modified the default Index view like following.

@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<p>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    { 
        <label for="file">Upload Image:</label>
        <input type="file" name="file" id="file"/>
        <input type="submit" value="Upload Image" />
    }
</p>

Here you can see that I have used Html.Begin form to create a form with multipart as we all know this attribute is required to have to upload any kind of file to the server. Also I have used the simple HTML file control and a submit button to submit a form. Now we are done with HTML so it’s time to write server-side code.

Following is a code for that.

[HttpPost]
       public ActionResult Index(HttpPostedFileBase file)
       {
           string path = System.IO.Path.Combine(Server.MapPath("~/Images"), System.IO.Path.GetFileName(file.FileName));
           file.SaveAs(path);
           ViewBag.Message = "File uploaded successfully";
           return View();
       }

As you can see I have create a new ActionResult Index with parameter and I have given HttpPost attribute to it to get all data from post method of form we are submitting and then I have written a code to save file in Images folder.

That’s it. We are dong with coding now its time to run the application. Once you press F5 it will look into the browser like following.

How to upload file in asp.net mvc3 

Now once you select file and click on upload image it will upload files and give a message like following.

File Upload in ASP.NET MVC 3

We are done. Hope you like it. Stay tuned for more updates. Till then Happy programming.

Shout it
Published Friday, April 20, 2012 2:22 AM by Jalpesh P. Vadgama

Comments

# File Upload in ASP.NET MVC3 - DotNetJalps | asp.net mvc | Scoop.it

Pingback from  File Upload in ASP.NET MVC3 - DotNetJalps | asp.net mvc | Scoop.it

# re: File Upload in ASP.NET MVC3

Saturday, April 21, 2012 1:07 AM by Rich Chang

Hi there,

Ok, nice job ! Would appreciate a followup with a more practical example ?

Can you also show how to make a User Control ( partialview )

for this and a viewModel to capture multiple files upload controls.

More than likely there will be more than one form on the page  and 1 file upload control to do the file upload from.

Thanks, Rich :)

# re: File Upload in ASP.NET MVC3

Saturday, April 21, 2012 1:36 AM by Rich Chang

Hi there,

Ok, nice job ! Would appreciate a followup with a more practical example ?

Can you also show how to make a User Control ( partialview )

for this and a viewModel to capture multiple files upload controls.

More than likely there will be more than one form on the page  and 1 file upload control to do the file upload from.

Thanks, Rich :)

# re: File Upload in ASP.NET MVC3

Wednesday, April 25, 2012 3:03 AM by Jalpesh P. Vadgama

@rich - sure in forth coming post I will demonstrate this.

# re: File Upload in ASP.NET MVC3

Wednesday, April 25, 2012 11:35 AM by Tino

Hello really nice job & very helpful !

But I have a little problem, I'm beginning with the using of ASP.NET and MVC3 and I don't know why but I have an error when I try to upload an image with your code, the error is :

"The connection was reset                

         The connection to the server was reset while the page was loading."

I don't understand because I'm using localhost...

Can you help me?

Tino (french student)

# re: File Upload in ASP.NET MVC3

Thursday, April 26, 2012 6:36 AM by Jalpesh P. Vadgama

@Tino- What kind of file you are uploading and what size of file you are uploading. If you are uploading large file then you need to max length of file in web.cofnig.

# re: File Upload in ASP.NET MVC3

Tuesday, July 17, 2012 3:29 AM by Prakash

When i use [HttpPost] in Action method my page not loaded and it shows the following error

Server Error in '/' Application.

The resource cannot be found.

Can any one help me ...

# re: File Upload in ASP.NET MVC3

Tuesday, July 17, 2012 3:34 AM by Prakash

Hi

When i use [HttpPost] In Action method my page not loaded it Shows the following Error

--Server Error in '/' Application.

--The resource cannot be found.

Can any one help me....

# re: File Upload in ASP.NET MVC3

Wednesday, July 18, 2012 9:30 AM by Jalpesh P. Vadgama

@prakash- Can you post the code here and which version of asp.net mvc 4 your are using.