Skip to content

Instantly share code, notes, and snippets.

@aliirz
Created September 14, 2013 15:33
Show Gist options
  • Save aliirz/6562961 to your computer and use it in GitHub Desktop.
Save aliirz/6562961 to your computer and use it in GitHub Desktop.
MVC File Upload Example
[Authorize(Roles = "Manager")]
public ActionResult Logo()
{
return View();
}
[Authorize(Roles = "Manager")]
[HttpPost]
public ActionResult Logo(HttpPostedFileBase file)
{
if (file != null && file.ContentLength != 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Logos"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Settings");
}
@{
ViewBag.Title = "Choose your Logo";
Layout = Url.Content("~/Views/Shared/_SettingsLayout.cshtml");
}
<div class="well">
@using (Html.BeginForm("Logo", "Manager", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal" }))
{
<div class="control-group">
<label class="control-label">
Upload Your company's Logo
</label>
<div class="controls">
<input type="file" name="file" />
</div>
</div>
<input type="submit" class="btn btn-info" value="Upload" />
}
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment