View:
@using (Html.BeginForm("AddDetails", "UsersDetails", null,
FormMethod.Post, new
{
encrypt = "multipart/form-data",
@class = "form-horizontal",
role = "form"
}))
{
@Html.AntiForgeryToken()
<label for="Picture">Upload Image:</label>
<input name="Picture" id="Picture" type="file" />
}
Controller action:
public ActionResult AddDetails(UsersDetailViewModel udv, FormCollection fc, HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
try
{
string path = Path.Combine(Server.MapPath("~/Uploads"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
else
{
ViewBag.Message = "You have not specified a file.";
}
ud.Picture = udv.Picture;
return View();
}
When I try to upload image then in upload input box it contain the whole path as c:\users\Adi\black.png
but I need only image name as black image.