Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. [Authorize(Roles = "Candidate, Admin")]
  4. public ActionResult Edit(
  5. [Bind(Include = "Id,UserName,Email,Name,Surname,OrasC,Localitate,StradaC,NrC,ZIPNumber,DataNasterii,Sex,Number,Tara,ImageID, File, FileName, ImageSize, ImageData, UserID, FilePath")]
  6. CVviewModel CV, Image img, Curriculum cr, FilePDF f, IEnumerable<HttpPostedFileBase> files)
  7. {
  8. var profileOwner = db.Users.Where(x => x.Id == LoggedUserId).Select(x => x.Id).SingleOrDefault();
  9.  
  10. if (User.Identity.IsAuthenticated && CV.Id.ToString() == profileOwner)
  11. {
  12. if (!ModelState.IsValid)
  13. {
  14. ApplicationUser u = new ApplicationUser
  15. {
  16. Id = CV.Id
  17. };
  18.  
  19. db.Users.Attach(u);
  20.  
  21. u.UserName = CV.Email;
  22. u.Email = CV.Email;
  23. u.Tara = CV.Tara;
  24. u.Name = CV.Name;
  25. u.Surname = CV.Surname;
  26. u.City = CV.OrasC;
  27. u.Localitate = CV.Localitate;
  28. u.Street = CV.StradaC;
  29. u.Nr = CV.NrC;
  30. u.ZIP = CV.ZIPNumber;
  31. u.Sex = CV.Sex;
  32. u.PhoneNumber = CV.Number;
  33. u.DataNasterii = CV.DataNasterii;
  34.  
  35. db.SaveChanges();
  36. }
  37.  
  38. #region ImageUpload
  39. if (files.ElementAt(0) != null && Request.Files.Count > 0)
  40. {
  41. var file1 = files.ElementAt(0);
  42.  
  43. if (file1 != null && file1.ContentLength > (2 * 1024 * 1024))
  44. {
  45. ModelState.AddModelError("Custom Error", "File must be less than 2 MB or doesn't exist");
  46. return View();
  47. }
  48.  
  49. if (!(file1.ContentType == "image/jpeg" || file1.ContentType == "image/gif" || file1.ContentType == "image/jpg"))
  50. {
  51. ModelState.AddModelError("CustomError", "Only jpeg and gif formats");
  52. return View();
  53. }
  54.  
  55. else
  56. {
  57. //collect all images
  58. var existImgRows = db.Images.Where(x => x.UserID == LoggedUserId).OrderByDescending(x => x.ImageID).Skip(0).Select(x => x);
  59.  
  60. if (existImgRows != null)
  61. {
  62. foreach (var item in existImgRows)
  63. {
  64. string fullPath = Request.MapPath("~/FilesDB/Images/" + item.FileName);
  65. if (System.IO.File.Exists(fullPath))
  66. {
  67. System.IO.File.Delete(fullPath);
  68. }
  69.  
  70. db.Entry(item).State = System.Data.Entity.EntityState.Deleted;
  71. }
  72.  
  73. string fileName = Guid.NewGuid() + Path.GetExtension(file1.FileName);
  74. string path = HttpContext.Server.MapPath("~/FilesDB/Images");
  75. file1.SaveAs(Path.Combine(Server.MapPath("~/FilesDB/Images"), fileName));
  76.  
  77. byte[] data = new byte[file1.ContentLength];
  78. file1.InputStream.Read(data, 0, file1.ContentLength);
  79.  
  80. img.ImageID = Guid.NewGuid();
  81. img.FileName = fileName;
  82. img.ImageSize = file1.ContentLength;
  83.  
  84. img.ImageData = data;
  85. img.UserID = LoggedUserId;
  86. img.FilePath = path;
  87.  
  88. db.Entry(img).State = System.Data.Entity.EntityState.Added;
  89. }
  90. }
  91. }
  92.  
  93. #endregion
  94. if (files.ElementAt(1) != null)
  95. {
  96. var file2 = files.ElementAt(1);
  97.  
  98. if (!(file2.ContentType == "application/pdf"))
  99. {
  100. ModelState.AddModelError("Custom Error", "Invalid format");
  101. return PartialView();
  102. }
  103. else
  104. {
  105. //collect all images
  106. var existImgRows = db.Files.Where(x => x.UserID == LoggedUserId).OrderByDescending(x => x.ID).Skip(0).Select(x => x);
  107.  
  108. if (existImgRows != null)
  109. {
  110. foreach (var item in existImgRows)
  111. {
  112. string fullPath = Request.MapPath("~/FilesDB/PDF/" + item.FileName);
  113. if (System.IO.File.Exists(fullPath))
  114. {
  115. System.IO.File.Delete(fullPath);
  116. }
  117.  
  118. db.Entry(item).State = System.Data.Entity.EntityState.Deleted;
  119. }
  120. }
  121.  
  122. string fileName = Guid.NewGuid() + Path.GetExtension(file2.FileName);
  123. string path = HttpContext.Server.MapPath("~/FilesDB/PDF");
  124. file2.SaveAs(Path.Combine(Server.MapPath("~/FilesDB/PDF"), fileName));
  125.  
  126. using (var reader = new System.IO.BinaryReader(file2.InputStream))
  127. {
  128. f.Content = reader.ReadBytes(file2.ContentLength);
  129. }
  130.  
  131. f.FileName = fileName;
  132. f.FileSize = file2.ContentLength;
  133.  
  134. f.UserID = LoggedUserId;
  135. f.FilePath = path;
  136.  
  137. db.Entry(f).State = System.Data.Entity.EntityState.Added;
  138. }
  139. }
  140.  
  141. try
  142. {
  143. db.SaveChanges();
  144. }
  145. catch (Exception)
  146. {
  147. ModelState.AddModelError("", "Something bad happened");
  148. }
  149.  
  150. }
  151. return RedirectToAction("_MainCV", "_CandidateForm");
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement