File Upload in ASP.Net MVC application

No.of Views3827
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : ASP.NET , ASP.NET MVC
This article will show how to upload a file from client to server location in ASP.Net MVC application in 5 easy steps.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will show how to upload a file from client to server location in ASP.Net MVC application in 5 easy steps.

Step 1:

Create an ASP.Net MVC application.
File->New->Project->web->ASP.Net MVC Application

Step 2:

Creating controller
a. Right click on Controller folder and add a new controller. 

Image Loading

FileUpload is name of the controller here. Don’t check the checkbox.

Description of HttpPostedFileBase class

For uploading the file in specified path at server HttpPostedFileBase class from System.Web namespace would be used.

HttpPostedFileBase class

a. This contains 4 properties
b. This contains one virtual method.
c. FileName property will be used to fetch the file name of the file.
d. SaveAs method will be used to save the file at server location.

using System;
using System.IO;

namespace System.Web
{
public abstract class HttpPostedFileBase
{
protected HttpPostedFileBase();


public virtual int ContentLength { get; }

public virtual string ContentType { get; }
public virtual string FileName { get; }
public virtual Stream InputStream { get; }
public virtual void SaveAs(string filename);
}
}

b. Type the below code in controller. This code will save the file at server. Location of the saved file would be E:\Temp directory.

FileUploadController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.IO;

namespace FileUploadSample.Controllers
{
public class FileUploadController : Controller
{

public ActionResult Edit()
{
return View();
}

public ActionResult Upload(HttpPostedFileBase file)
{
var fileName = Path.GetFileName(file.FileName);
file.SaveAs(@"E:\Temp\"+fileName);

return RedirectToAction("Index");
}

public ActionResult Index()
{
return Redirect("Home/Index");
}

}
}

Step 3

Creating view
a. Right click on Edit action and select Add view 

Image Loading

b. Leave the default setting

Image Loading

c. In Edit.aspx, inside a content place holder, include the following Html tag. This is including control for file to upload.

<form action="/FileUpload/Upload" method="post" enctype="multipart/form-data"><label>Filename: <input type="file" name="file" /></label><input type="submit" value="Submit" />

If you see the action parameter for form, it is URL for the action which is actually uploading the file at server.=

d. Entire Edit.aspx will look like

Edit.aspx

Image Loading

Step 4:

Adding link at the main page for file uploading

a. Open site.master in Views->Shared Folder
b. Add below line in menu tag

Image Loading

c. So the menu element will look like,

Image Loading

Step 5:

Press F5 to run with debug 

Image Loading

 

Click on File Upload

Image Loading

Sample Project Source

Download source files -269 kb

Conclusion:
This article showed how to upload a file from client to server location in ASP.Net MVC application.Happy Coding. Thank you

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
By:HarvDate Of Posted:12/10/2010 11:04:48 AM
mr
Haven't tried this yet, but looks like it is just what I am looking for. I imagine it would be possible to upload multiple files via a comma separated list for example
By:aspforceDate Of Posted:11/24/2010 11:23:55 AM
FileUpload on Windows Azure
Can I use this approach on Windows Azure?
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top