public ActionResult DeleteFile(int?Id) { using (var tran = db.Database.BeginTransaction()) { try { string filePath = null; long? UserID = Convert.ToInt64(User.Identity.Name); FileCollection fileCol = db.FileCollections.FirstOrDefault(x => x.Id == Id && x.UserID == UserID); filePath = Server.MapPath("~/" + fileCol.FilePath); db.FileCollections.Remove(fileCol); db.SaveChanges(); if (!string.IsNullOrEmpty(filePath)) { if (System.IO.File.Exists(filePath)) { long length = new System.IO.FileInfo(filePath).Length; System.IO.File.Delete(filePath); UserUsage userUsage = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); userUsage.UsedStorage = userUsage.UsedStorage - Mathmatics.ConvertBytesToMegabytes(length); db.SaveChanges(); } } tran.Commit(); } catch (Exception er) { tran.Rollback(); } } return(RedirectToAction("UploadImage")); }
public ActionResult Create(Product product, HttpPostedFileBase file) { long?UserID = Convert.ToInt64(User.Identity.Name); if (ModelState.IsValid) { string pic = ""; if (file != null) { if (file.ContentLength != 0) { pic = System.IO.Path.GetFileName(file.FileName); pic = Guid.NewGuid() + "_" + pic; string path = System.IO.Path.Combine(Server.MapPath("~/Content/upload/" + UserID + "/Commerce"), pic); // file is uploaded file.SaveAs(path); // product.ProductImage = "Content/upload/" + UserID + "/Commerce/" + pic; product.ProductImage = "/xImage/" + UserID + "/Commerce/" + pic; UserUsage userUsage = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); userUsage.UsedStorage += Mathmatics.ConvertBytesToMegabytes(file.ContentLength); db.SaveChanges(); } } product.UserID = UserID; db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CatagoryID = new SelectList(db.ProductCatagories.Where(x => x.UserID == UserID), "Id", "CatagoryName"); return(View(product)); }
public ActionResult UploadImage(HttpPostedFileBase file) { string pic = ""; var UserID = Convert.ToInt64(User.Identity.Name); using (var trans = db.Database.BeginTransaction()) { try { if (file != null) { if (file.ContentLength != 0) { FileCollection fileCol = new FileCollection(); if (!System.IO.Directory.Exists(Server.MapPath("~/Content/upload"))) { System.IO.Directory.CreateDirectory(Server.MapPath("~/Content/upload")); } pic = System.IO.Path.GetFileName(file.FileName); string picExtention = System.IO.Path.GetExtension(pic); pic = Guid.NewGuid() + "_a" + picExtention; string path = System.IO.Path.Combine(Server.MapPath("~/Content/upload/" + UserID), pic); // file uploading file.SaveAs(path); // string filePath = "http://" + Request.ServerVariables["HTTP_HOST"] + "/Content/filecollection/" + pic; string filePath = "Content/upload/" + UserID + "/" + pic; fileCol.FilePath = filePath; fileCol.UserID = UserID; db.FileCollections.Add(fileCol); db.SaveChanges(); UserUsage userUsage = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); userUsage.UsedStorage += Mathmatics.ConvertBytesToMegabytes(file.ContentLength); db.SaveChanges(); trans.Commit(); TempData["Msg"] = "<h2 class='text-center' style='display: block; color: green'>File has been uploaded</h2><p><b>Link to Copy: </b> " + filePath + "</p>"; return(RedirectToAction("UploadImage")); } else { TempData["Msg"] = "<h2 class='text-center' style='display: block; color: green'>File not uploaded. Please try agian</h2>"; return(RedirectToAction("UploadImage")); } } } catch (Exception er) { trans.Rollback(); } } TempData["Msg"] = "<h2 class='text-center' style='display: block; color: green'>There is an error please try again.</h2>"; return(RedirectToAction("UploadImage")); }
public ActionResult Edit(User user, HttpPostedFileBase file) { if (ModelState.IsValid) { using (var trasn = db.Database.BeginTransaction()) { try { long? UserID = Convert.ToInt64(User.Identity.Name); User detail = db.Users.Find(UserID); string OldPicPath = detail.ProfilePic; string pic = ""; if (file != null) { if (file.ContentLength != 0) { pic = System.IO.Path.GetFileName(file.FileName); pic = Guid.NewGuid() + "_" + pic; string path = System.IO.Path.Combine(Server.MapPath("~/Content/upload/" + UserID), pic); // file is uploaded file.SaveAs(path); detail.ProfilePic = "Content/upload/" + UserID + "/" + pic; UserUsage userUsage = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); userUsage.UsedStorage += Mathmatics.ConvertBytesToMegabytes(file.ContentLength); db.SaveChanges(); } } detail.FirstName = user.FirstName; detail.LastName = user.LastName; detail.Email = user.Email; detail.Address = user.Address; detail.CellNumber = user.CellNumber; detail.Password = user.Password; db.SaveChanges(); Session["UserProfilePath"] = detail.ProfilePic; Session["UserFirstName"] = !string.IsNullOrEmpty(detail.FirstName) ? detail.FirstName.ToUpper() : "Hi!"; if (!string.IsNullOrEmpty(OldPicPath)) { string fileToDelete = Server.MapPath("~/" + OldPicPath); if (!string.IsNullOrEmpty(fileToDelete)) { if (System.IO.File.Exists(fileToDelete)) { long length = new System.IO.FileInfo(fileToDelete).Length; System.IO.File.Delete(fileToDelete); UserUsage PriHistory = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); PriHistory.UsedStorage = PriHistory.UsedStorage - Mathmatics.ConvertBytesToMegabytes(length); db.SaveChanges(); } } } trasn.Commit(); return(RedirectToAction("Index")); } catch (Exception er) { trasn.Rollback(); } } } return(View(user)); }
public async Task <ActionResult> Edit(ShopUser shopUser, HttpPostedFileBase file) { using (var Tran = db.Database.BeginTransaction()) { try { // validate current sub domain domainIndentifier = new DomainIdentifier(Request.Url.Host, Request.ServerVariables["SERVER_NAME"]); var User = domainIndentifier.GetUserRelatedDomain(); UserID = User != null ? User.Id : 0; ViewBag.UserID = UserID; Session["CurrentUserID"] = UserID; //// check user is login or not if (string.IsNullOrEmpty(Convert.ToString(Session["WebloggedUserID"]))) { return(Redirect("/Account/Login")); } // bussiness logic long ShopUserID = Convert.ToInt64(Session["WebloggedUserID"]); ViewBag.ShopUserID = ShopUserID; if (ModelState.IsValid) { ShopUser Customer = db.ShopUsers.FirstOrDefault(x => x.Id == ShopUserID); if (!System.IO.Directory.Exists(Server.MapPath("~/Content/upload/" + UserID + "/Commerce"))) { System.IO.Directory.CreateDirectory(Server.MapPath("~/Content/upload/" + UserID + "/ShopUsers")); } if (file != null) { if (file.ContentLength > 0) { string FileExtention = Path.GetExtension(file.FileName); string FileName = Guid.NewGuid().ToString() + DateTime.Now.ToString("yyyydd") + ShopUserID + FileExtention; string FileSavePath = System.IO.Path.Combine(Server.MapPath("~/Content/upload/" + UserID + "/ShopUsers"), FileName); file.SaveAs(FileSavePath); Customer.ProfileImagePath = "Content/upload/" + UserID + "/ShopUsers" + FileName; } } int IsSavedIntoStorageTable = 0; string PreProfilePath = Customer.ProfileImagePath; Customer.CurrentAddress = shopUser.CurrentAddress; Customer.DisplayName = shopUser.DisplayName; Customer.Email = shopUser.Email; Customer.FirstName = shopUser.FirstName; Customer.LastName = shopUser.LastName; Customer.Mobile = shopUser.Mobile; Customer.Password = shopUser.Password; Customer.PermanentAddress = shopUser.PermanentAddress; Customer.Phone = shopUser.Phone; Customer.SecPhone = shopUser.SecPhone; int isSavedIntoShopUser = await db.SaveChangesAsync(); // Insert Data if (!string.IsNullOrEmpty(PreProfilePath)) { string fileToDelete = Server.MapPath("~/" + PreProfilePath); if (!string.IsNullOrEmpty(fileToDelete)) { if (System.IO.File.Exists(fileToDelete)) { long length = new System.IO.FileInfo(fileToDelete).Length; UserUsage PriHistory = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); PriHistory.UsedStorage = PriHistory.UsedStorage - Mathmatics.ConvertBytesToMegabytes(length); IsSavedIntoStorageTable = db.SaveChanges(); // Insert Data System.IO.File.Delete(fileToDelete); } } } if (isSavedIntoShopUser > 0 && IsSavedIntoStorageTable > 0) { Tran.Commit(); return(RedirectToAction("Detail")); } return(View(shopUser)); } } catch (Exception er) { Tran.Rollback(); } return(View(shopUser)); } }
public ActionResult Edit(SiteSetting siteSetting, HttpPostedFileBase file) { using (var Transaction = db.Database.BeginTransaction()) { long?UserID = Convert.ToInt64(User.Identity.Name); var setting = db.SiteSettings.FirstOrDefault(x => x.UserID == UserID); try { if (ModelState.IsValid) { string OldLogoPath = setting.Logo; string pic = ""; if (file != null) { if (file.ContentLength != 0) { pic = System.IO.Path.GetFileName(file.FileName); pic = Guid.NewGuid() + "_" + pic; string path = System.IO.Path.Combine(Server.MapPath("~/Content/upload/" + UserID), pic); // file is uploaded file.SaveAs(path); setting.Logo = "Content/upload/" + UserID + "/" + pic; UserUsage userUsage = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); userUsage.UsedStorage += Mathmatics.ConvertBytesToMegabytes(file.ContentLength); db.SaveChanges(); } } setting.Email = siteSetting.Email; setting.OfficeAddress = siteSetting.OfficeAddress; setting.Phone = siteSetting.Phone; setting.SmtpEmail = siteSetting.SmtpEmail; setting.SmtpIsEnableSSL = siteSetting.SmtpIsEnableSSL; setting.SmtpPassword = siteSetting.SmtpPassword; setting.SmtpPort = siteSetting.SmtpPort; setting.SmtpServer = siteSetting.SmtpServer; db.SaveChanges(); if (!string.IsNullOrEmpty(OldLogoPath)) { string fileToDelete = Server.MapPath("~/" + OldLogoPath); if (!string.IsNullOrEmpty(fileToDelete)) { if (System.IO.File.Exists(fileToDelete)) { long length = new System.IO.FileInfo(fileToDelete).Length; UserUsage PriHistory = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); PriHistory.UsedStorage = PriHistory.UsedStorage - Mathmatics.ConvertBytesToMegabytes(length); db.SaveChanges(); System.IO.File.Delete(fileToDelete); } } } Transaction.Commit(); return(RedirectToAction("Index")); } } catch (Exception er) { Transaction.Rollback(); } return(View(setting)); } }
public ActionResult Edit(Product productModel, HttpPostedFileBase file) { long?UserID = Convert.ToInt64(User.Identity.Name); var product = db.Products.FirstOrDefault(x => x.UserID == UserID && x.Id == productModel.Id); using (var Tran = db.Database.BeginTransaction()) { try { if (ModelState.IsValid) { string oldImage = product.ProductImage; string pic = ""; if (file != null) { if (file.ContentLength != 0) { pic = System.IO.Path.GetFileName(file.FileName); pic = Guid.NewGuid() + "_" + pic; string path = System.IO.Path.Combine(Server.MapPath("~/Content/upload/" + UserID + "/Commerce"), pic); // file is uploaded file.SaveAs(path); // product.ProductImage = "Content/upload/" + UserID + "/Commerce/" + pic; product.ProductImage = "/xImage/" + UserID + "/Commerce/" + pic; UserUsage userUsage = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); userUsage.UsedStorage += Mathmatics.ConvertBytesToMegabytes(file.ContentLength); db.SaveChanges(); } } product.UserID = UserID; product.Name = productModel.Name; product.Description = productModel.Description; product.Discount = productModel.Discount; product.Price = productModel.Price; productModel.CatagoryID = productModel.CatagoryID; db.SaveChanges(); if (!string.IsNullOrEmpty(oldImage)) { string fileToDelete = Server.MapPath("~/" + oldImage); if (!string.IsNullOrEmpty(fileToDelete)) { if (System.IO.File.Exists(fileToDelete)) { long length = new System.IO.FileInfo(fileToDelete).Length; UserUsage PriHistory = db.UserUsages.FirstOrDefault(x => x.UserID == UserID); PriHistory.UsedStorage = PriHistory.UsedStorage - Mathmatics.ConvertBytesToMegabytes(length); db.SaveChanges(); System.IO.File.Delete(fileToDelete); } } } return(RedirectToAction("Index")); } Tran.Commit(); ViewBag.CatagoryID = new SelectList(db.ProductCatagories.Where(x => x.UserID == UserID), "Id", "CatagoryName"); } catch (Exception er) { Tran.Rollback(); } } return(View(product)); }