//todo: make this method for one or multiple files protected async Task <ICollection <Models.Image> > HandleFileSelected(IFileListEntry[] files) { Models.Image _image = new Models.Image(); file = files.FirstOrDefault(); using (MemoryStream ms = new MemoryStream((int)file.Size + 1)) { await file.Data.CopyToAsync(ms); _image.Data = ms.ToArray(); } _image.ImageName = file.Name; string byte64 = Convert.ToBase64String(_image.Data); int extensionIndex = _image.ImageName.IndexOf('.') + 1; string extension = _image.ImageName.Remove(0, extensionIndex); ImagesData.Add(new ImageData() { name = _image.ImageName, string64Data = new string("data:image/" + extension + "; base64," + byte64) }); await ImagesDataChanged.InvokeAsync(ImagesData); ModelImages.Add(_image); _image = new Models.Image(); return(ModelImages); }
public static string GetUrlImage(this ModelImages model) { return((int)model switch { -1 => $"image/{ModelImages.Package.ToString().ToLower()}.png", >= 0 and <= 10 => $"image/model_{(int)model}.png", >= 100 and <= 123 => $"image/model_{(int)model}.png", _ => string.Empty });
//To Remove Old images of Brands public bool RemoveImage(int imageId) { var exist = context.Images.FirstOrDefault(x => x.imageId == imageId); if (exist == null) { return(false); } try { ModelImages m = new ModelImages() { imageId = imageId }; context.Entry(m).State = Microsoft.EntityFrameworkCore.EntityState.Deleted; context.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public int Update(EditViewModel model) { BrandModel b = new BrandModel(); b.modelId = model.modelId; b.model_name = model.model_name; b.mob_type = model.mob_type; b.RAM = model.RAM + " " + model.RAMCapacity; b.internal_storage = model.internal_storage + " " + model.ROMCapacity; b.Display = model.Display; b.Battery = model.Battery; b.Price = model.Price; b.SimType = model.SimType; b.back_cam = model.back_cam; b.Front_Cam = model.Front_Cam; b.front_flash = model.front_flash; b.FingerPrint = model.FingerPrint; b.Networktype = model.Networktype; b.LaunchDate = model.LaunchDate; b.ProcessorName = model.ProcessorName; b.PhoneId = model.PhoneId; b.Android = model.Android; //Handling Images Uploading ExistingImages.Length - UpcomingPhotos.Length more Images if (model.Photos != null && model.Photos.Count > 0) { string uniqueFileName = null; int length = context.Images.Count(x => x.modelId == model.modelId); int ImagesLength = model.Photos.Count; if (length + ImagesLength > 4) { foreach (IFormFile photo in model.Photos) { if (4 - length == 0) { break; } else { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "image"); uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName; ModelImages i = new ModelImages() { modelId = model.modelId, Image_Path = uniqueFileName, }; context.Images.Add(i); context.SaveChanges(); string filePath = Path.Combine(uploadsFolder, uniqueFileName); // Use CopyTo() method provided by IFormFile interface to // copy the file to wwwroot/images folder photo.CopyTo(new FileStream(filePath, FileMode.Create)); length++; } } } else { foreach (IFormFile photo in model.Photos) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "image"); uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName; ModelImages i = new ModelImages() { modelId = model.modelId, Image_Path = uniqueFileName, }; context.Images.Add(i); context.SaveChanges(); string filePath = Path.Combine(uploadsFolder, uniqueFileName); // Use CopyTo() method provided by IFormFile interface to // copy the file to wwwroot/images folder photo.CopyTo(new FileStream(filePath, FileMode.Create)); } } } context.BrandModel.Update(b); context.SaveChanges(); return(b.modelId); }
public async Task <int> addModel(ModelViewModel model) { BrandModel m = new BrandModel() { model_name = model.model_name, mob_type = model.mob_type, RAM = model.RAM + " " + model.RAMCapacity, internal_storage = model.internal_storage + " " + model.ROMCapacity, Display = model.Display, Battery = model.Battery, Price = model.Price, SimType = model.SimType, //require end back_cam = model.back_cam, Front_Cam = model.Front_Cam, front_flash = model.front_flash, FingerPrint = model.FingerPrint, Networktype = model.Networktype, PhoneId = model.PhoneId, ProcessorName = model.ProcessorName, LaunchDate = model.LaunchDate, Android = model.Android }; context.BrandModel.Add(m); context.SaveChanges(); string uniqueFileName = null; if (model.Photos != null && model.Photos.Count > 0) { // Loop thru each selected file foreach (IFormFile photo in model.Photos) { // The file must be uploaded to the images folder in wwwroot // To get the path of the wwwroot folder we are using the injected // IHostingEnvironment service provided by ASP.NET Core string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "image"); uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName; // To make sure the file name is unique we are appending a new // GUID value and and an underscore to the file name ModelImages i = new ModelImages() { modelId = m.modelId, Image_Path = uniqueFileName, }; context.Images.Add(i); string filePath = Path.Combine(uploadsFolder, uniqueFileName); // Use CopyTo() method provided by IFormFile interface to // copy the file to wwwroot/images folder photo.CopyTo(new FileStream(filePath, FileMode.Create)); } context.SaveChanges(); } var Stores = util.GetAllStores(); List <Stock> stock = new List <Stock>(); foreach (var store in Stores) { Stock s = new Stock() { modelId = m.modelId, Quantity = 0, store_id = store.store_id }; stock.Add(s); } await context.Stock.AddRangeAsync(stock); await context.SaveChangesAsync(); return(m.modelId); }
public ActionResult Update(Model x) { if (ModelState.IsValid) { Model xdb = db.Model.Include("Brand").Include("ModelImages").FirstOrDefault(c => c.Id == x.Id); xdb.BrandId = x.BrandId; xdb.Color = x.Color; xdb.Condition = x.Condition; xdb.Doors = x.Doors; xdb.Engine = x.Engine; xdb.EngineLayout = x.EngineLayout; xdb.FuelType = x.FuelType; xdb.HorsePower = x.HorsePower; xdb.Mass = x.Mass; xdb.Mileage = x.Mileage; xdb.Name = x.Name; xdb.PriceDaily = x.PriceDaily; xdb.Seats = x.Seats; xdb.Transmission = x.Transmission; xdb.Year = x.Year; xdb.DriveTrain = x.DriveTrain; xdb.hasABS = x.hasABS; xdb.hasAlloyWheels = x.hasAlloyWheels; xdb.hasCC = x.hasCC; xdb.hasConditioner = x.hasConditioner; xdb.hasESP = x.hasESP; xdb.hasLeatherInterior = x.hasLeatherInterior; xdb.hasPSensors = x.hasPSensors; xdb.hasXenon = x.hasXenon; xdb.Description = x.Description; xdb.ModifiedDate = DateTime.Now; xdb.AdminModifiedId = (int)Session["AdminId"]; db.Entry(xdb).State = EntityState.Modified; db.SaveChanges(); if (x.ImageFile[0] != null) { using (DBContext db = new DBContext()) { foreach (var item in db.ModelImages.Where(c => c.ModelId == xdb.Id)) { string oldImagePath = Path.Combine(Server.MapPath("~/Uploads"), item.Name); System.IO.File.Delete(oldImagePath); db.ModelImages.Remove(item); } db.SaveChanges(); } foreach (HttpPostedFileBase image in x.ImageFile) { string imageName = DateTime.Now.ToString("ddMMyyyyHHmmssffff") + Regex.Replace(image.FileName, "[^A-Za-z0-9.]", ""); string imagePath = Path.Combine(Server.MapPath("~/Uploads"), imageName); image.SaveAs(imagePath); ModelImages w = new ModelImages(); w.Name = imageName; w.ModelId = xdb.Id; db.ModelImages.Add(w); db.SaveChanges(); } db.SaveChanges(); } int AdminId = (int)Session["AdminId"]; string ConnectionId = db.Admin.FirstOrDefault(c => c.Id == AdminId).ConnectionId; GlobalHost.ConnectionManager.GetHubContext <NotifyHub>().Clients.AllExcept(ConnectionId).modelUpdate(((Models.Admin)Session["Admin"]).FullName, DateTime.Now.ToString("HH:mm"), xdb.Name, "Model"); TempData["Update"] = "Update"; return(RedirectToAction("Index")); } Model xdb2 = db.Model.Include("Brand").Include("ModelImages").FirstOrDefault(p => p.Id == x.Id); ViewBag.ModelImages = db.ModelImages.Where(c => c.ModelId == xdb2.Id).ToList(); ViewBag.Brand = db.Brand.ToList(); ViewBag.ModelPage = true; TempData["Update-Error"] = "Update-Error"; return(View(x)); }
public ActionResult Create(Model x) { int AdminId = (int)Session["AdminId"]; if (ModelState.IsValid) { Model xdb = new Model(); xdb.AdminId = x.AdminId; xdb.PostDate = DateTime.Now; xdb.BrandId = x.BrandId; xdb.Color = x.Color; xdb.Condition = x.Condition; xdb.Doors = x.Doors; xdb.Engine = x.Engine; xdb.EngineLayout = x.EngineLayout; xdb.FuelType = x.FuelType; xdb.HorsePower = x.HorsePower; xdb.Mass = x.Mass; xdb.Mileage = x.Mileage; xdb.Name = x.Name; xdb.PriceDaily = x.PriceDaily; xdb.Seats = x.Seats; xdb.Transmission = x.Transmission; xdb.DriveTrain = x.DriveTrain; xdb.Year = x.Year; xdb.hasABS = x.hasABS; xdb.hasAlloyWheels = x.hasAlloyWheels; xdb.hasCC = x.hasCC; xdb.hasConditioner = x.hasConditioner; xdb.hasESP = x.hasESP; xdb.hasLeatherInterior = x.hasLeatherInterior; xdb.hasPSensors = x.hasPSensors; xdb.hasXenon = x.hasXenon; xdb.Description = x.Description; if (db.AdminSettings.FirstOrDefault(c => c.AdminId == AdminId).alwaysActive) { xdb.isActive = true; } else { xdb.isActive = false; } db.Model.Add(xdb); db.SaveChanges(); if (x.ImageFile != null) { foreach (HttpPostedFileBase image in x.ImageFile) { if (image == null) { xdb.ModelImages = null; } else { string imageName = DateTime.Now.ToString("ddMMyyyyHHmmssffff") + Regex.Replace(image.FileName, "[^A-Za-z0-9.]", ""); string imagePath = Path.Combine(Server.MapPath("~/Uploads"), imageName); image.SaveAs(imagePath); ModelImages w = new ModelImages(); w.Name = imageName; w.ModelId = xdb.Id; db.ModelImages.Add(w); db.SaveChanges(); } } } TempData["Create"] = "Create"; string ConnectionId = db.Admin.FirstOrDefault(c => c.Id == AdminId).ConnectionId; GlobalHost.ConnectionManager.GetHubContext <NotifyHub>().Clients.AllExcept(ConnectionId).modelCreate(((Models.Admin)Session["Admin"]).FullName, DateTime.Now.ToString("HH:mm"), xdb.Name, "Model"); return(RedirectToAction("Index", "Model")); } ViewBag.ModelPage = true; ViewBag.Admin = (int)Session["AdminId"]; ViewBag.Brand = db.Brand.ToList(); TempData["Create-Error"] = "Create-Error"; return(View(x)); }