public async Task <IActionResult> Start()
        {
            var user = await userManager.GetUserAsync(User);

            PracticeLog pl = new PracticeLog();

            pl.PracticeLogID     = Guid.NewGuid().ToString();
            pl.PracticeStartTime = DateTime.Now;
            pl.UserID            = user.Id;
            pl.Date       = DateTime.Now;
            pl.DayOfWeek  = DateTime.Now.DayOfWeek;
            pl.InProgress = true;


            // setting new sessions for started practice
            HttpContext.Session.SetString("logID", pl.PracticeLogID);
            HttpContext.Session.SetString("start", $"Practice started at " +
                                          $"{pl.PracticeStartTime.ToShortTimeString()}");

            context.PracticeLog.Add(pl);
            context.SaveChanges();

            return(RedirectToAction("Index", pl));
        }
Пример #2
0
        public async Task <IActionResult> SaveImage(Image image)
        {
            var user = await userManager.GetUserAsync(User);

            foreach (var file in Request.Form.Files)
            {
                if (image.UserID == null)
                {
                    //Image object is created and its properties set
                    Image img2 = new Image
                    {
                        ImageTitle = file.FileName,
                        ImageID    = image.ImageID,
                        UserID     = user.Id
                    };

                    //To grab the image data the code creates a
                    //MemoryStream and copies the uploaded image data into it using the CopyTo() method
                    MemoryStream ms = new MemoryStream();
                    file.CopyTo(ms);

                    //MemoryStream is converted into a byte array using its ToArray() method.
                    //This byte array is stored in the ImageData property of the Image object.
                    img2.ImageData = ms.ToArray();

                    ms.Close();
                    ms.Dispose();

                    context.Images.Add(img2);
                    context.SaveChanges();
                }
                else
                {
                    Image img2 = new Image
                    {
                        ImageTitle = file.FileName,
                        ImageID    = image.ImageID,
                        UserID     = image.UserID
                    };

                    MemoryStream ms = new MemoryStream();
                    file.CopyTo(ms);

                    img2.ImageData = ms.ToArray();

                    ms.Close();
                    ms.Dispose();

                    context.Images.Update(img2);
                    context.SaveChanges();
                }
            }

            Image  img             = context.Images.OrderByDescending(i => i.ImageID).FirstOrDefault();
            string imageBase64Data = Convert.ToBase64String(img.ImageData);
            string imageDataURL    = string.Format("data:image/jpg;base64,{0}", imageBase64Data);

            ViewBag.ImageDataUrl = imageDataURL;

            return(RedirectToAction("ProfilePicture"));
        }