public ActionResult UpdateAvatar(HttpPostedFileBase file)
{
ManageMessageId? message;
if (file != null && file.ContentLength > 0)
{
// extract only the filename
var fileName = User.Identity.GetUserId() + Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/Uploads/avatars"), fileName);
file.SaveAs(path);
string id = User.Identity.GetUserId();
// удаление старого аватара с сервера
string old = DataManager.User.GetUserAvatar(id);
if (old != "/Content/images/devault-avatar.png")
{
path = Path.Combine(Server.MapPath(old));
FileInfo fi = new FileInfo(path);
fi.Delete();
}
// обновление нового аватара
DataManager.User.UpdateUserAvatar(id, "/Uploads/avatars/" + fileName);
message = ManageMessageId.AvatarUpdated;
}
else
{
message = ManageMessageId.Error;
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index", new { Message = message });
}