public async Task <IActionResult> AddImage(int?id, IFormFile uploads) { if (id != null) { if (uploads != null) { ElectricTrain train = await _context.Electrics.Where(x => x.id == id).FirstOrDefaultAsync(); byte[] p1 = null; using (var fs1 = uploads.OpenReadStream()) using (var ms1 = new MemoryStream()) { fs1.CopyTo(ms1); p1 = ms1.ToArray(); } train.ImageMimeTypeOfData = uploads.ContentType; train.Image = p1; _context.Electrics.Update(train); _context.SaveChanges(); return(RedirectToAction(nameof(InModered))); } } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(int id, [Bind("id,Name, Model, VagonsCountP,MaxSpeed,Imgsrc, DepotTrain, DepotCity, LastKvr, Created, Plant, PlaceKvr, User")] ElectricTrain electricTrain) { var remoteIpAddres = Request.HttpContext.Connection.RemoteIpAddress.ToString(); Users userlog = _context.User.Where(x => x.IpAddress.Contains(remoteIpAddres)).FirstOrDefault(); if (userlog != null && userlog.Status == "true") { ViewBag.user = userlog; } if (id != electricTrain.id) { return(NotFound()); } //if (ModelState.IsValid) //{ try { var depocity = _context.Depots.Where(x => x.Name == electricTrain.DepotTrain).Select(x => x.Addres).FirstOrDefault(); electricTrain.DepotCity = depocity; ElectricTrain train = _context.Electrics.Where(x => x.id == electricTrain.id).FirstOrDefault(); train.Name = electricTrain.Name; train.VagonsCountP = electricTrain.VagonsCountP; train.MaxSpeed = electricTrain.MaxSpeed; train.DepotCity = electricTrain.DepotCity; train.DepotTrain = electricTrain.DepotTrain; train.LastKvr = electricTrain.LastKvr; train.Created = electricTrain.Created; train.Plant = electricTrain.Plant; train.PlaceKvr = electricTrain.PlaceKvr; _context.Update(train); await _context.SaveChangesAsync(); Users user = await _context.User.Where(x => x.Name == electricTrain.User).FirstOrDefaultAsync(); SendMessage(user); } catch (DbUpdateConcurrencyException) { if (!ElectricTrainExists(electricTrain.id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); //} //return View(electricTrain); }
public async void CreateAction([FromBody] string data) { var remoteIpAddres = Request.HttpContext.Connection.RemoteIpAddress.ToString(); Users user = _context.User.Where(x => x.IpAddress.Contains(remoteIpAddres)).FirstOrDefault(); if (user != null && user.Status == "true") { ViewBag.user = user; } ElectricTrain electric = JsonConvert.DeserializeObject <ElectricTrain>(data); _context.Add(electric); await _context.SaveChangesAsync(); }
public async Task <IActionResult> Create([Bind("id,Name, Model, VagonsCountP,MaxSpeed,Imgsrc, DepotTrain, LastKvr, Created, Plant, PlaceKvr, User")] ElectricTrain electricTrain) { var remoteIpAddres = Request.HttpContext.Connection.RemoteIpAddress.ToString(); Users userlog = _context.User.Where(x => x.IpAddress.Contains(remoteIpAddres)).FirstOrDefault(); string username = userlog.Name; int userid = userlog.Id; if (userlog != null && userlog.Status == "true") { ViewBag.user = userlog; } try { var depo = _context.Depots.Where(x => x.Name == electricTrain.DepotTrain).Select(x => x.Addres).FirstOrDefault(); electricTrain.DepotCity = depo; electricTrain.User = username; electricTrain.UserId = userid; electricTrain.IsProof = false.ToString(); _context.Add(electricTrain); await _context.SaveChangesAsync(); Users user = await _context.User.Where(x => x.Name == electricTrain.User).FirstOrDefaultAsync(); //SendMessage(user); ElectricTrain train = _context.Electrics.Where(x => x.Name == electricTrain.Name && x.Model == electricTrain.Model && x.User == electricTrain.User).FirstOrDefault(); TempData["Train"] = train.id; return(RedirectToAction(nameof(AddImageForm))); } catch (Exception exp) { string trace = exp.ToString(); try { FileStream fileStreamLog = new FileStream(@"Exception.log", FileMode.Append); for (int i = 0; i < trace.Length; i++) { byte[] array = Encoding.Default.GetBytes(trace.ToString()); fileStreamLog.Write(array, 0, array.Length); } fileStreamLog.Close(); } catch (Exception e) { Trace.WriteLine(e.StackTrace); } } return(View(electricTrain)); }
private void Button_Click(object sender, RoutedEventArgs e) { try { ElectricTrain electricTrain = new ElectricTrain { Name = namebox.Text, MaxSpeed = Convert.ToInt32(maxspeed.Text), VagonsCountP = vagonbox.Text, Imgsrc = imgstr.Text }; Post.Send("ElectricTrains", "CreateAction", this, electricTrain); }catch (Exception exp) { Trace.WriteLine(exp.ToString()); MessageBox.Show(exp.ToString(), "Error"); } }
public FileContentResult GetImage(int id) { ElectricTrain train = _context.Electrics .FirstOrDefault(g => g.id == id); try { if (train != null) { var file = File(train.Image, train.ImageMimeTypeOfData); return(file); } else { return(null); } }catch (Exception exp) { Trace.WriteLine(exp.ToString()); } return(null); }