I am trying to save uploaded image in database and show in view using .Net MVC but no success.
Here is my code.
Model:
public class TeamStartup
{
public string thumb { get; set; }
}
Controller:
public class CityDetailController : Controller
{
//Save Team Member StartUp Picture
private string SaveTeamMemberPicture(HttpPostedFileBase thumb)
{
string fileName = default(string);
string savedPath = default(string);
string[] validExtensions = new string[4] { ".png", ".jpg", ".gif", ".jpeg" };
if (thumb != null && thumb.ContentLength > 0)
{
fileName = Path.GetFileName(thumb.FileName);
var fileExt = new FileInfo(fileName).Extension;
if (!validExtensions.Contains(fileExt.ToLower()))
{
ModelState.AddModelError(string.Empty, "Image type Invalid");
}
if (thumb.ContentLength > 1048576)
{
ModelState.AddModelError(string.Empty, "file size limit exceeds (Max 1MB)");
}
if (ModelState.IsValid)
{
fileName = DateTime.Now.Ticks.ToString();
var path = Path.Combine(Server.MapPath(@System.Configuration.ConfigurationManager.AppSettings["UploadsPath"] + "/Avatars/City"), fileName);
thumb.SaveAs(path);
savedPath = fileName;
}
}
return savedPath;
}
}
It doesn't save and show anything in database, Any help would be appreciated.
Thank You.