public ActionResult Create(Bottle Bottle, HttpPostedFileBase BtlImg) { if (ModelState.IsValid) { if (BtlImg != null && BtlImg.ContentLength > 0) { int length = BtlImg.ContentLength; byte[] tempArray = new byte[length]; BtlImg.InputStream.Read(tempArray, 0, length); Bitmap bmp = ImageFunctions.resizeImage (new Bitmap(BtlImg.InputStream), new Size() { Height = 800, Width = 800}); using (MemoryStream ms = new MemoryStream()) { bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); tempArray = ms.ToArray(); } Bottle.BottleImage.BottleImg = tempArray; } db.Bottles.Add(Bottle); db.SaveChanges(); return RedirectToAction("Index"); } return View(Bottle); }
public static Bottle Deserialize(string serialized) { string[] split = serialized.Split('#'); for (int i = 0; i < split.Count(); i++) { if (string.IsNullOrEmpty(split[i])) { split[i] = string.Empty; } } Bottle b = new Bottle(); b.BottleDrinkDetail = new BottleDrinkDetail(); b.BottleDetail = new BottleDetail(); b.BottleImage = new BottleImage(); b.BottleOrigin = new BottleOrigin(); try { b.Id = int.Parse(split[0]); b.BottleDrinkDetail.AlcoholType = split[1]; b.BottleDrinkDetail.Alcohol = split[2]; b.BottleDrinkDetail.Content = split[3]; b.BottleDrinkDetail.Age = int.Parse(split[4]); b.BottleDetail.Shell = split[5]; b.BottleDetail.Name = split[6]; b.BottleDetail.Shape = split[7]; b.BottleDetail.Color = split[8]; b.BottleDetail.Material = split[9]; b.BottleOrigin.Manufacturer = split[10]; b.BottleOrigin.City = split[11]; b.BottleOrigin.Country = split[12]; b.BottleOrigin.Continent = split[13]; b.BottleDetail.Note = split[14]; } catch (IndexOutOfRangeException ex) { return null; } return b; }
public static string Serialize(Bottle b) { if (b.Id.ToString() == null) { b.Id = 0; } if (b.BottleDrinkDetail.AlcoholType == null) { b.BottleDrinkDetail.AlcoholType = " "; } if (b.BottleDrinkDetail.Alcohol == null) { b.BottleDrinkDetail.Alcohol = " "; } if (b.BottleDrinkDetail.Content == null) { b.BottleDrinkDetail.Content = " "; } if (b.BottleDrinkDetail.Age.ToString() == null) { b.BottleDrinkDetail.Age = 0; } if (b.BottleDetail.Shell == null) { b.BottleDetail.Shell = " "; } if (b.BottleDetail.Name == null) { b.BottleDetail.Name = " "; } if (b.BottleDetail.Shape == null) { b.BottleDetail.Shape = " "; } if (b.BottleDetail.Color == null) { b.BottleDetail.Color = " "; } if (b.BottleDetail.Material == null) { b.BottleDetail.Material = " "; } if (b.BottleOrigin.Manufacturer == null) { b.BottleOrigin.Manufacturer = " "; } if (b.BottleOrigin.City == null) { b.BottleOrigin.City = " "; } if (b.BottleOrigin.Country == null) { b.BottleOrigin.Country = " "; } if (b.BottleOrigin.Continent == null) { b.BottleOrigin.Continent = " "; } if (b.BottleDetail.Note == null) { b.BottleDetail.Note = " "; } return b.Id.ToString().Replace('#', ' ') + "#" + b.BottleDrinkDetail.AlcoholType.Replace('#', ' ') + "#" + b.BottleDrinkDetail.Alcohol.Replace('#', ' ') + "#" + b.BottleDrinkDetail.Content.Replace('#', ' ') + "#" + b.BottleDrinkDetail.Age.ToString().Replace('#', ' ') + "#" + b.BottleDetail.Shell.Replace('#', ' ') + "#" + b.BottleDetail.Name.Replace('#', ' ') + "#" + b.BottleDetail.Shape.Replace('#', ' ') + "#" + b.BottleDetail.Color.Replace('#', ' ') + "#" + b.BottleDetail.Material.Replace('#', ' ') + "#" + b.BottleOrigin.Manufacturer.Replace('#', ' ') + "#" + b.BottleOrigin.City.Replace('#', ' ') + "#" + b.BottleOrigin.Country.Replace('#', ' ') + "#" + b.BottleOrigin.Continent.Replace('#', ' ') + "#" + b.BottleDetail.Note.Replace('#', ' ') + "#" + "\n"; }
public ActionResult Post() { Stream s = Request.InputStream; StreamReader sr = new StreamReader(s); Bottle b = new Bottle(); while (!sr.EndOfStream) { string bottle = sr.ReadLine(); b = Bottle.Deserialize(bottle); if (b != null) { context.Bottles.Add(b); context.SaveChanges(); } return Content("1"); } return Content("0"); }