public void update(HttpPostedFileWrapper file) { ProjectType prc = new ProjectType(); if (Request.Params["IdUpdate"].Length > 0) { int id = int.Parse(Request.Params["IdUpdate"]); prc = _projectType.GetProjectById(id); string url = prc.Image; if (Request.Params["nametype"].Length > 0) { if (file != null) { var allowedExtensions = new[] { ".JPG", ".PNG", ".JPEG", ".GIF" }; var fileName = Path.GetFileName(file.FileName); //getting only file name(ex-ganesh.jpg) var ext = (Path.GetExtension(file.FileName)).ToUpper(); //getting the extension(ex-.jpg) if (allowedExtensions.Contains(ext)) //check what type of extension { string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension string myfile = UnitilHelper.convertToUnSign(name) + DateTime.Now.Millisecond + ext; //appending the name with id string path = System.IO.Path.Combine(Server.MapPath("/Upload"), myfile); url = "Upload/" + myfile; file.SaveAs(path); } } prc.NameType = Request.Params["nametype"]; prc.Description = Request.Params["description"]; prc.Keyword = Request.Params["Keyword"]; prc.MetaDescription = Request.Params["MetaDescription"]; prc.Image = url; _projectType.UpdateProjectType(prc); _unitOfWork.SaveChanges(); } } Response.Redirect("Index"); }
public void Insert(FormCollection fc, HttpPostedFileWrapper file) { if (file != null && fc["nametype"].ToString() != "") { ProjectType _pro = new ProjectType(); var allowedExtensions = new[] { ".JPG", ".PNG", ".JPEG", ".GIF" }; var fileName = Path.GetFileName(file.FileName); //getting only file name(ex-ganesh.jpg) var ext = (Path.GetExtension(file.FileName)).ToUpper(); //getting the extension(ex-.jpg) if (allowedExtensions.Contains(ext)) //check what type of extension { string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension string myfile = UnitilHelper.convertToUnSign(name) + DateTime.Now.Millisecond + ext; //appending the name with id string path = System.IO.Path.Combine(Server.MapPath("/Upload"), myfile); string url = "Upload/" + myfile; try { file.SaveAs(path); _pro.NameType = fc["nametype"].ToString(); _pro.Keyword = fc["Keyword"].ToString(); _pro.MetaDescription = fc["MetaDescription"].ToString(); _pro.Description = fc["description"].ToString() == "" ? "Đang cập nhật" : fc["description"].ToString(); _pro.Image = url; _pro.IsDeleted = false; _projectType.AddProjectType(_pro); _unitOfWork.SaveChanges(); } catch { } } } Response.Redirect("Index"); }