public ActionResult UploadFiles() { // Checking no of files injected in Request object if (Request.Files.Count > 0) { try { var list = JsonConvert.DeserializeObject <List <DbTables.TagBox> >(Request.Form["tags"]); //deserialize and turn json objects (the tags) into actual tag class // Get all files from Request object var files = Request.Files; for (var i = 0; i < files.Count; i++) { var file = files[i]; var picture = new DbTables.File { FileName = Path.GetFileName(file.FileName), FileType = DbTables.FileType.Picture, ContentType = file.ContentType, Tags = list, User = new List <ApplicationUser>(), Children = new List <DbTables.Child>(), UploadDate = DateTime.Now.Date, Published = true }; var caption = Request.Form["caption"]; if (!string.IsNullOrEmpty(caption)) { picture.Caption = caption; } foreach (var tagBox in list) { //get users and children from tag boxes. add to the relations of the image if (tagBox.type == "user") { var user = _context.Users.Find(tagBox.Id); if (user != null) { picture.User.Add(user); } } else { if (tagBox.Id != null) { //if nothing is selected id comes in as null, do nothing then var child = _context.Children.Find(int.Parse(tagBox.Id)); if (child != null) { picture.Children.Add(child); } } } } using (var reader = new BinaryReader(file.InputStream)) { picture.Content = reader.ReadBytes(file.ContentLength); } _context.Files.Add(picture); _context.SaveChanges(); } // Returns message that successfully uploaded return(Json("Filopplastning var en suksess!")); } catch (Exception ex) { return(Json("Ooops, det skjedde en feil: " + ex.Message)); } } return(Json("Ingen filer valgt")); }
/// <summary> /// Upload background image /// </summary> /// <returns>Succsess or error view</returns> public ActionResult BackgroundUpload() { if (Request.Files.Count > 0) { try { var backgroundObj = new DbTables.BackgroundImage(); var background = _context.BackgroundImage.ToList(); if (background.Any()) { backgroundObj = background.First(); } else { backgroundObj = new DbTables.BackgroundImage { Enabeled = true, Image = new DbTables.File() }; _context.BackgroundImage.Add(backgroundObj); } // Get all files from Request object var files = Request.Files; for (var i = 0; i < files.Count; i++) { var file = files[i]; var fileUpload = new DbTables.File { FileName = Path.GetFileName(file.FileName), ContentType = file.ContentType, Temporary = false, FileType = DbTables.FileType.BackgroundImage }; using (var reader = new BinaryReader(file.InputStream)) { fileUpload.Content = reader.ReadBytes(file.ContentLength); } _context.Files.Add(fileUpload); backgroundObj.Image = fileUpload; } _context.SaveChanges(); ViewBag.Success = "Filen(e) ble sukksessfult lastet opp"; return(PartialView("_BackgroundImagePartial", backgroundObj)); } catch (Exception ex) { ViewBag.Error = "Error: " + ex.Message; var backgrounderr = new DbTables.BackgroundImage(); var backgroundl = _context.BackgroundImage.ToList(); if (backgroundl.Any()) { backgrounderr = backgroundl.First(); } return(PartialView("_BackgroundImagePartial", backgrounderr)); } } ViewBag.Error = "Ingen fil valgt"; var backgroundError = new DbTables.BackgroundImage(); var backgroundL = _context.BackgroundImage.ToList(); if (backgroundL.Any()) { backgroundError = backgroundL.First(); } return(PartialView("_BackgroundImagePartial", backgroundError)); }
/// <summary> /// upload carousel images and videos /// </summary> /// <returns></returns> public ActionResult CarouselUpload() { if (Request.Files.Count > 0) //check that there are files to get { try { var carouselObj = new DbTables.Carousel(); var carousel = _context.Carousel.ToList(); if (carousel.Any()) //if it already exists, clear the current files and delete them { carouselObj = carousel.First(); carouselObj.CarouselItems.Clear(); var deleteCarousel = _context.Files.Where( s => (s.FileType == DbTables.FileType.CarouselImage) || (s.FileType == DbTables.FileType.CarouselVideo)); if (deleteCarousel.Any()) { _context.Files.RemoveRange(deleteCarousel); //remove many at once. } } else { carouselObj = new DbTables.Carousel { Enabeled = true, CarouselItems = new List <DbTables.File>() }; _context.Carousel.Add(carouselObj); } // Get all files from Request object var files = Request.Files; for (var i = 0; i < files.Count; i++) { var file = files[i]; var fileUpload = new DbTables.File { FileName = Path.GetFileName(file.FileName), ContentType = file.ContentType, Temporary = false }; using (var reader = new BinaryReader(file.InputStream)) { fileUpload.Content = reader.ReadBytes(file.ContentLength); } if (file.ContentType.Contains("video")) //it's a video, add as video type { fileUpload.FileType = DbTables.FileType.CarouselVideo; } else { fileUpload.FileType = DbTables.FileType.CarouselImage; //it's and image } _context.Files.Add(fileUpload); carouselObj.CarouselItems.Add(fileUpload); } _context.SaveChanges(); ViewBag.Success = "Filen(e) ble sukksessfult lastet opp"; return(PartialView("_ImageCarouselPartial", carouselObj)); } catch (Exception ex) { ViewBag.Error = "Error: " + ex.Message; var carouselErr = new DbTables.Carousel(); var carousell = _context.Carousel.ToList(); if (carousell.Any()) { carouselErr = carousell.First(); } return(PartialView("_ImageCarouselPartial", carouselErr)); } } ViewBag.Error = "Ingen fil valgt"; var carouselError = new DbTables.Carousel(); var carouselL = _context.Carousel.ToList(); if (carouselL.Any()) { carouselError = carouselL.First(); } return(PartialView("_ImageCarouselPartial", carouselError)); }
/// <summary> /// upload terms of use document /// </summary> /// <returns>Succsess or failure partial view</returns> public ActionResult TermsUpload() { if (Request.Files.Count > 0) //check that there are actually files in the request { try { var Terms = new DbTables.TermsOfUse(); var TermsList = _context.TermsOfUse.ToList(); if (TermsList.Any()) //if it exist already, get it { Terms = TermsList.First(); } // Get all files from Request object var files = Request.Files; for (var i = 0; i < files.Count; i++) //get the files object { var file = files[i]; if (Terms.Id == 0) //add the file, it does not exist { var TermsFile = new DbTables.File { FileName = Path.GetFileName(file.FileName), FileType = DbTables.FileType.PDF, ContentType = file.ContentType, Temporary = false }; using (var reader = new BinaryReader(file.InputStream)) { TermsFile.Content = reader.ReadBytes(file.ContentLength); } _context.Files.Add(TermsFile); Terms.Terms = TermsFile; Terms.Enabeled = true; _context.TermsOfUse.Add(Terms); _context.SaveChanges(); } else { var TermsFile = Terms.Terms; //it exists, just change values TermsFile.FileName = Path.GetFileName(file.FileName); TermsFile.FileType = DbTables.FileType.PDF; TermsFile.ContentType = file.ContentType; using (var reader = new BinaryReader(file.InputStream)) { TermsFile.Content = reader.ReadBytes(file.ContentLength); } _context.SaveChanges(); } } ViewBag.Success = "Filen ble sukksessfult lastet opp"; return(PartialView("_TermsOfUserPartial", Terms)); } catch (Exception ex) { ViewBag.Error = "Error: " + ex.Message; var Terms = new DbTables.TermsOfUse(); var TermsList = _context.TermsOfUse.ToList(); if (TermsList.Any()) { Terms = TermsList.First(); } return(PartialView("_TermsOfUserPartial", Terms)); } } ViewBag.Error = "Ingen fil valgt"; var TermsError = new DbTables.TermsOfUse(); var TermsListError = _context.TermsOfUse.ToList(); if (TermsListError.Any()) { TermsError = TermsListError.First(); } return(PartialView("_TermsOfUserPartial", TermsError)); }
public ActionResult ProfilePictureUpload() { // Checking no of files injected in Request object if (Request.Files.Count > 0) { try { var store = new UserStore <ApplicationUser>(_context); var manager = new UserManager <ApplicationUser>(store); var currentUser = manager.FindByIdAsync(User.Identity.GetUserId()).Result; //the current user var userId = Request.Form["userid"]; var ProfileImageWidth = 300; var ThumbnailImageWidth = 40; var type = Request.Form["type"]; //ApplicationUser user = _context.Users.Find(userId); var fileId = 0; var employee = new ApplicationUser(); var child = new DbTables.Child(); IList <DbTables.File> pictures = new List <DbTables.File>(); if (type == "employee") { employee = _context.Users.Find(userId); if (currentUser.RoleNr >= employee.RoleNr) { //user not the current user or rolenumber to high, not allowed to change profile picture if (currentUser.Id != userId) { return (Json( new { error = "Du har ikke lov til å endre dette profilbildet", success = "false" })); } } pictures = employee.Pictures; } else if (type == "child") { child = _context.Children.Find(int.Parse(userId)); pictures = child.Pictures; } //var pictures = user.Pictures; var profPic = new DbTables.File(); var thumbNail = new DbTables.ThumbNail(); if (pictures.Any()) { foreach (var pic in pictures) { if (pic.FileType == DbTables.FileType.Profile) { profPic = pic; thumbNail = pic.ThumbNail; } } } // Get all files from Request object var files = Request.Files; for (var i = 0; i < files.Count; i++) { var file = files[i]; if (profPic.Content == null) { var picture = new DbTables.File(); //save profile picture if (employee.Email != null) { picture = new DbTables.File { FileName = Path.GetFileName(file.FileName), FileType = DbTables.FileType.Profile, ContentType = file.ContentType, User = new List <ApplicationUser> { employee } } } ; else { picture = new DbTables.File { FileName = Path.GetFileName(file.FileName), FileType = DbTables.FileType.Profile, ContentType = file.ContentType, Children = new List <DbTables.Child> { child } } }; byte[] content; using (var reader = new BinaryReader(file.InputStream)) { content = reader.ReadBytes(file.ContentLength); } Bitmap bmp; using (var ms = new MemoryStream(content)) { bmp = new Bitmap(ms); } var ratio = bmp.Width / (double)bmp.Height; var height = (int)(ProfileImageWidth / ratio); var newImg = ResizeImage(bmp, ProfileImageWidth, height); byte[] cntnt; using (var stream = new MemoryStream()) { newImg.Save(stream, ImageFormat.Png); cntnt = stream.ToArray(); } picture.Content = cntnt; _context.Files.Add(picture); //save thumbnail var thumbnail = new DbTables.ThumbNail { ThumbNailName = Path.GetFileName(file.FileName), ContentType = file.ContentType, FileType = DbTables.FileType.Thumbnail, File = picture //User = new List<ApplicationUser> {user} }; var Theight = (int)(ThumbnailImageWidth / ratio); var newThumb = ResizeImage(bmp, ThumbnailImageWidth, Theight); byte[] Tcntnt; using (var stream = new MemoryStream()) { newThumb.Save(stream, ImageFormat.Png); Tcntnt = stream.ToArray(); } thumbnail.Content = Tcntnt; _context.ThumbNails.Add(thumbnail); if (employee.Email != null) { employee.Thumbnail = thumbnail; _context.Entry(employee).State = EntityState.Modified; } else { child.Thumbnail = thumbnail; _context.Entry(child).State = EntityState.Modified; } _context.SaveChanges(); //fileId = picture.FileId; } else { //Profile picture profPic.FileName = Path.GetFileName(file.FileName); profPic.ContentType = file.ContentType; byte[] content; using (var reader = new BinaryReader(file.InputStream)) { content = reader.ReadBytes(file.ContentLength); } Bitmap bmp; using (var ms = new MemoryStream(content)) { bmp = new Bitmap(ms); } var ratio = bmp.Width / (double)bmp.Height; var height = (int)(ProfileImageWidth / ratio); var newImg = ResizeImage(bmp, ProfileImageWidth, height); byte[] cntnt; using (var stream = new MemoryStream()) { newImg.Save(stream, ImageFormat.Png); cntnt = stream.ToArray(); } profPic.Content = cntnt; _context.Entry(profPic).State = EntityState.Modified; //thumbnail thumbNail.ThumbNailName = Path.GetFileName(file.FileName); thumbNail.ContentType = file.ContentType; var Theight = (int)(ThumbnailImageWidth / ratio); var newThumb = ResizeImage(bmp, ThumbnailImageWidth, Theight); byte[] Tcntnt; using (var stream = new MemoryStream()) { newThumb.Save(stream, ImageFormat.Png); Tcntnt = stream.ToArray(); } thumbNail.Content = Tcntnt; _context.Entry(thumbNail).State = EntityState.Modified; _context.SaveChanges(); //fileId = profPic.FileId; } } var user = _context.Users.Find(userId); var ProfileImage = new DbTables.File(); var Picture = user.Pictures.Where(s => s.FileType == DbTables.FileType.Profile); if (Picture.Any()) { ProfileImage = Picture.First(); } // Returns message that successfully uploaded return(PartialView("_ProfileImagePartial", ProfileImage)); //return Json("Filopplastning var en suksess!"); } catch (Exception ex) { return(Json("Ooops, det skjedde en feil: " + ex.Message)); } } return(Json("Ingen filer valgt")); }
// // GET: /Manage/Index public async Task <ActionResult> Index(ManageMessageId?message) { var background = new DbTables.BackgroundImage(); var backgroundList = _context.BackgroundImage.ToList(); if (backgroundList.Any()) { background = backgroundList.First(); if (background.Enabeled) { ViewBag.Style = "background:url('/File/Background?id=" + background.Image.FileId + "') no-repeat center center fixed;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;background-size: cove;overflow-x: hidden;"; ViewBag.BackGround = "background-color:transparent;"; } } ViewBag.StatusMessage = message == ManageMessageId.ChangePasswordSuccess ? "Ditt passord har blitt endred." : message == ManageMessageId.SetPasswordSuccess ? "Ditt passord har blitt sett." : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set." : message == ManageMessageId.Error ? "En feil oppstod." : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added." : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed." : ""; var userId = User.Identity.GetUserId(); var indexModel = new IndexViewModel { HasPassword = HasPassword(), PhoneNumber = await UserManager.GetPhoneNumberAsync(userId), TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId), Logins = await UserManager.GetLoginsAsync(userId), BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId) }; var user = _context.Users.Find(userId); var ProfileImage = new DbTables.File(); var pictures = user.Pictures.Where(s => s.FileType == DbTables.FileType.Profile); if (pictures.Any()) { ProfileImage = pictures.First(); } var profileModel = new changeProfileModel //change profile user info { Fname = user.Fname, Lname = user.Lname, City = user.Adress.City, StreetAdress = user.Adress.StreetAdress, PostCode = user.Adress.PostCode, State = user.Adress.County, Phone = user.Phone, Id = user.Id, File = ProfileImage, BirthNumber = user.BirthNumber }; var model = new ProfileViewModel { Index = indexModel, Profile = profileModel }; return(View(model)); //return view with updated values }