Oct 29 2009

How to Upload Files in Asp.net MVC

FileUpload Control

Model folder contains:  Purchase.dbml, IMediaRepository.cs Interface and MediaRepository.cs class file

Controller: Create New controller MediaController

View: Create New Folder name is Media

if controller name MediaController

view folder name is Media (see the below figure round controller and view folder name )

Note: Create Folder in Project SolutionExplorer name is Uploads

mvc-fileupload

mvc-fileupload

IMediaRepository.cs

public interface IMediaRepository

{

void Add(Media media);

void Save();

}

MediaRepository

public class MediaRepository :IMediaRepository

{

PurchaseDataContext db = new PurchaseDataContext();

public void Add(Media media)

{

db.Medias.InsertOnSubmit(media);

}

public void Save()

{

db.SubmitChanges();

}

}

MediaController

public string filename;

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Create(Media media)

{

try

{

// TempData["Id"] = Request.Form["Id"];

// TODO: Add insert logic here

foreach (string inputTagName in Request.Files)

{

HttpPostedFileBase file = Request.Files[inputTagName];

if (file.ContentLength > 0)

{

string filePath = Path.Combine(HttpContext.Server.MapPath(”../Uploads“)

, Path.GetFileName(file.FileName));

file.SaveAs(filePath);

filename  = file.FileName;

}

}

//TempData["FileName"] = media.FileName;

media.FileName = filename;  //to get filename Remmber

mediaRepository.Add(media);

mediaRepository.Save();

return RedirectToAction(”Index”);

}

catch

{

return View();

}

}

View Folder

Create.aspx (Media/Create.aspx)

Remmeber this line

<% using (Html.BeginForm(”Create”, “Media”, FormMethod.Post, new { enctype = “multipart/form-data” }))

{%>

—————————————————————

asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>

<h2>Create</h2>

<%= Html.ValidationSummary(”Create was unsuccessful. Please correct the errors and try again.”) %>

<%–  <form action=”/Media/Create” method=”post”>–%>

<%– <% using (Html.BeginForm()) {%>–%>

<% using (Html.BeginForm(”Create“, “Media“, FormMethod.Post, new { enctype = “multipart/form-data” }))

{%>

<fieldset>

<legend>Fields</legend>

<p>

<label for=”Id”>Id:</label>

<%= Html.TextBox(”Id“) %>

<%= Html.ValidationMessage(”Id”, “*”) %>

</p>

<p>

<label for=”FileName”>FileName:</label>

<input type=”file“  name=”FileName” size=”23″/>

</p>

<p>

<label for=”Title”>Title:</label>

<%= Html.TextBox(”Title“) %>

<%= Html.ValidationMessage(”Title”, “*”) %>

</p>

<p>

<label for=”Description”>Description:</label>

<%= Html.TextBox(”Description“) %>

<%= Html.ValidationMessage(”Description”, “*”) %>

</p>

<p>

<label for=”EntryDate”>EntryDate:</label>

<%= Html.TextBox(”EntryDate“) %>

<%= Html.ValidationMessage(”EntryDate”, “*”) %>

</p>

<p>

<input type=”submit” value=”Create” />

</p>

</fieldset>

<% } %>

<div>

<%=Html.ActionLink(”Back to List”, “Index”) %>

</div>

<%–</form>–%>

</asp:Content>

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Reply

Alibi3col theme by Themocracy