public void Insert(FormCollection fc, HttpPostedFileWrapper file) { if (file != null && fc["NameAds"].ToString() != "") { Ads _pro = new Ads(); 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 urlads = "Upload/" + myfile; try { file.SaveAs(path); _pro.Image = urlads; _pro.IsDelete = false; _pro.IsShow = fc["optionsRadios"] == "SetHot" ? true : false; _pro.Name = fc["NameAds"]; _pro.URL = fc["Url"]; _adsService.InsertAds(_pro); _unitOfWork.SaveChanges(); } catch { } } } Response.Redirect("Index"); }
public void update(HttpPostedFileWrapper file, FormCollection fc) { Ads prc = new Ads(); if (Request.Params["IdUpdate"].Length > 0) { int id = int.Parse(Request.Params["IdUpdate"]); prc = _adsService.GetById(id); string url = prc.Image; if (Request.Params["NameAds"].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 = fc["NameAds"]; prc.URL = fc["Url"]; prc.IsShow = fc["optionsRadios"] == "SetHot" ? true : false; prc.Image = url; _adsService.UpdateAds(prc); _unitOfWork.SaveChanges(); } } Response.Redirect("Index"); }