Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [ValidateAntiForgeryToken]
- [Authorize(Roles = "Candidate, Admin")]
- public ActionResult Edit(
- [Bind(Include = "Id,UserName,Email,Name,Surname,OrasC,Localitate,StradaC,NrC,ZIPNumber,DataNasterii,Sex,Number,Tara,ImageID, File, FileName, ImageSize, ImageData, UserID, FilePath")]
- CVviewModel CV, Image img, Curriculum cr, FilePDF f, IEnumerable<HttpPostedFileBase> files)
- {
- var profileOwner = db.Users.Where(x => x.Id == LoggedUserId).Select(x => x.Id).SingleOrDefault();
- if (User.Identity.IsAuthenticated && CV.Id.ToString() == profileOwner)
- {
- if (!ModelState.IsValid)
- {
- ApplicationUser u = new ApplicationUser
- {
- Id = CV.Id
- };
- db.Users.Attach(u);
- u.UserName = CV.Email;
- u.Email = CV.Email;
- u.Tara = CV.Tara;
- u.Name = CV.Name;
- u.Surname = CV.Surname;
- u.City = CV.OrasC;
- u.Localitate = CV.Localitate;
- u.Street = CV.StradaC;
- u.Nr = CV.NrC;
- u.ZIP = CV.ZIPNumber;
- u.Sex = CV.Sex;
- u.PhoneNumber = CV.Number;
- u.DataNasterii = CV.DataNasterii;
- db.SaveChanges();
- }
- #region ImageUpload
- if (files.ElementAt(0) != null && Request.Files.Count > 0)
- {
- var file1 = files.ElementAt(0);
- if (file1 != null && file1.ContentLength > (2 * 1024 * 1024))
- {
- ModelState.AddModelError("Custom Error", "File must be less than 2 MB or doesn't exist");
- return View();
- }
- if (!(file1.ContentType == "image/jpeg" || file1.ContentType == "image/gif" || file1.ContentType == "image/jpg"))
- {
- ModelState.AddModelError("CustomError", "Only jpeg and gif formats");
- return View();
- }
- else
- {
- //collect all images
- var existImgRows = db.Images.Where(x => x.UserID == LoggedUserId).OrderByDescending(x => x.ImageID).Skip(0).Select(x => x);
- if (existImgRows != null)
- {
- foreach (var item in existImgRows)
- {
- string fullPath = Request.MapPath("~/FilesDB/Images/" + item.FileName);
- if (System.IO.File.Exists(fullPath))
- {
- System.IO.File.Delete(fullPath);
- }
- db.Entry(item).State = System.Data.Entity.EntityState.Deleted;
- }
- string fileName = Guid.NewGuid() + Path.GetExtension(file1.FileName);
- string path = HttpContext.Server.MapPath("~/FilesDB/Images");
- file1.SaveAs(Path.Combine(Server.MapPath("~/FilesDB/Images"), fileName));
- byte[] data = new byte[file1.ContentLength];
- file1.InputStream.Read(data, 0, file1.ContentLength);
- img.ImageID = Guid.NewGuid();
- img.FileName = fileName;
- img.ImageSize = file1.ContentLength;
- img.ImageData = data;
- img.UserID = LoggedUserId;
- img.FilePath = path;
- db.Entry(img).State = System.Data.Entity.EntityState.Added;
- }
- }
- }
- #endregion
- if (files.ElementAt(1) != null)
- {
- var file2 = files.ElementAt(1);
- if (!(file2.ContentType == "application/pdf"))
- {
- ModelState.AddModelError("Custom Error", "Invalid format");
- return PartialView();
- }
- else
- {
- //collect all images
- var existImgRows = db.Files.Where(x => x.UserID == LoggedUserId).OrderByDescending(x => x.ID).Skip(0).Select(x => x);
- if (existImgRows != null)
- {
- foreach (var item in existImgRows)
- {
- string fullPath = Request.MapPath("~/FilesDB/PDF/" + item.FileName);
- if (System.IO.File.Exists(fullPath))
- {
- System.IO.File.Delete(fullPath);
- }
- db.Entry(item).State = System.Data.Entity.EntityState.Deleted;
- }
- }
- string fileName = Guid.NewGuid() + Path.GetExtension(file2.FileName);
- string path = HttpContext.Server.MapPath("~/FilesDB/PDF");
- file2.SaveAs(Path.Combine(Server.MapPath("~/FilesDB/PDF"), fileName));
- using (var reader = new System.IO.BinaryReader(file2.InputStream))
- {
- f.Content = reader.ReadBytes(file2.ContentLength);
- }
- f.FileName = fileName;
- f.FileSize = file2.ContentLength;
- f.UserID = LoggedUserId;
- f.FilePath = path;
- db.Entry(f).State = System.Data.Entity.EntityState.Added;
- }
- }
- try
- {
- db.SaveChanges();
- }
- catch (Exception)
- {
- ModelState.AddModelError("", "Something bad happened");
- }
- }
- return RedirectToAction("_MainCV", "_CandidateForm");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement