public void Insert(FormCollection fc, HttpPostedFileWrapper file) { if (file != null && fc["name"].ToString() != "") { Contractor _pro = new Contractor(); 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.Name = fc["name"].ToString(); _pro.LinkContent = fc["linkcontent"].ToString(); _pro.Image = url; _pro.MetaDescription = "Chung cu"; _pro.Keyword = "Chung cu"; _pro.IsDeleted = false; _contractorServices.insertContractor(_pro); _unitOfWork.SaveChanges(); } catch { } } } Response.Redirect("Index"); }
public void update(HttpPostedFileWrapper file) { Contractor prc = new Contractor(); if (Request.Params["IdUpdate"].Length > 0) { int id = int.Parse(Request.Params["IdUpdate"]); prc = _contractorServices.GetContractorById(id); string url = prc.Image; if (Request.Params["name"].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.Name = Request.Params["name"]; prc.LinkContent = Request.Params["LinkContent"]; prc.Image = url; _contractorServices.updateContractor(prc); _unitOfWork.SaveChanges(); } } Response.Redirect("Index"); }