コード例 #1
0
        public void TestImage_Extension_PNG_JPEG_Result_True(string extension)
        {
            CheckImage checkImage = new CheckImage();
            string     FileName   = $"testImage.{extension}";
            bool       result     = checkImage.checkExtension(FileName);

            Assert.True(result);
        }
コード例 #2
0
        public ActionResult Create([Bind(Include = "LastName, FirstMidName, EnrollmentDate")] Student student, HttpPostedFileBase upload)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (upload != null && upload.ContentLength > 0)
                    {
                        //getting the image
                        string fileName = System.IO.Path.GetExtension(upload.FileName);
                        //call of the verification class CheckImage
                        CheckImage check = new CheckImage();

                        //call of the verification Extension method
                        bool extensionIsTrue = check.checkExtension(fileName);

                        if (extensionIsTrue == false)
                        {
                            ViewBag.ErrorExtension = ErrorMessages.ErrorExtension();
                            return(View());
                        }


                        //call of the verfication Size method
                        bool sizeIsCorrect = check.checkSize(upload.ContentLength);

                        if (sizeIsCorrect == false)
                        {
                            ViewBag.ErrorSize = ErrorMessages.ErrorSize();
                            return(View());
                        }

                        var avatar = new FileImage
                        {
                            FileName    = Path.GetFileName(upload.FileName),
                            FileType    = FileType.Avatar,
                            ContentType = upload.ContentType
                        };
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            avatar.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        student.Files = new List <FileImage> {
                            avatar
                        };
                    }
                    db.Students.Add(student);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return(View(student));
        }
コード例 #3
0
        public ActionResult Edit(int?id, string[] selectedCourses, HttpPostedFileBase upload)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var instructorToUpdate = db.Instructors
                                     .Include(i => i.OfficeAssignment)
                                     .Include(i => i.Courses)
                                     .Where(i => i.ID == id)
                                     .Single();

            if (TryUpdateModel(instructorToUpdate, "",
                               new string[] { "LastName", "FirstMidName", "HireDate", "OfficeAssignment" }))
            {
                try
                {
                    if (String.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment.Location))
                    {
                        instructorToUpdate.OfficeAssignment = null;
                    }

                    UpdateInstructorCourses(selectedCourses, instructorToUpdate);
                    if (upload != null && upload.ContentLength > 0)
                    {
                        #region CheckImage
                        CheckImage check = new CheckImage();
                        //getting the image
                        string fileName = System.IO.Path.GetExtension(upload.FileName);
                        //call of the verification Extension method
                        bool extensionIsTrue = check.checkExtension(fileName);

                        if (extensionIsTrue == false)
                        {
                            ViewBag.ErrorType = ErrorMessages.ErrorExtension();
                            Instructor instructor = db.Instructors
                                                    .Include(s => s.FileImage)
                                                    .Include(i => i.OfficeAssignment)
                                                    .Include(i => i.Courses)
                                                    .Where(i => i.ID == id)
                                                    .Single();
                            PopulateAssignedCourseData(instructor);
                            return(View(instructor));
                        }

                        //call of the verfication Size method
                        bool sizeIsCorrect = check.checkSize(upload.ContentLength);

                        if (sizeIsCorrect == false)
                        {
                            ViewBag.ErrorSize = ErrorMessages.ErrorSize();
                            Instructor instructor = db.Instructors
                                                    .Include(s => s.FileImage)
                                                    .Include(i => i.OfficeAssignment)
                                                    .Include(i => i.Courses)
                                                    .Where(i => i.ID == id)
                                                    .Single();
                            PopulateAssignedCourseData(instructor);
                            return(View(instructor));
                        }
                        #endregion
                        //Remove the the previous image
                        if (instructorToUpdate.FileImage.Any(f => f.FileType == FileType.Avatar))
                        {
                            db.FileImages.Remove(instructorToUpdate.FileImage.First(f => f.FileType == FileType.Avatar));
                        }

                        UploadImage uploadImage = new UploadImage();
                        FileImage   avatar      = uploadImage.Upload(upload);

                        instructorToUpdate.FileImage = new List <FileImage> {
                            avatar
                        };
                    }
                    db.Entry(instructorToUpdate).State = EntityState.Modified;

                    db.SaveChanges();

                    return(View(viewName: "Details", model: instructorToUpdate));
                }
                catch (RetryLimitExceededException /* dex */)
                {
                    //Log the error (uncomment dex variable name and add a line here to write a log.
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                }
            }
            PopulateAssignedCourseData(instructorToUpdate);
            return(View(instructorToUpdate));
        }
コード例 #4
0
        public ActionResult EditPost(int?id, HttpPostedFileBase upload)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var studentToUpdate = db.Students.Find(id);

            if (TryUpdateModel(studentToUpdate, "",
                               new string[] { "LastName", "FirstMidName", "EnrollmentDate" }))
            {
                try
                {
                    if (upload != null && upload.ContentLength > 0)
                    {
                        //string typeFile = Path.GetExtension(upload.FileName);
                        CheckImage check = new CheckImage();
                        //getting the image
                        string fileName = System.IO.Path.GetExtension(upload.FileName);

                        //call of the verification Extension method
                        bool extensionIsTrue = check.checkExtension(fileName);

                        if (extensionIsTrue == false)
                        {
                            ViewBag.ErrorType = ErrorMessages.ErrorExtension();
                            Student student = db.Students.Include(s => s.Files).SingleOrDefault(s => s.ID == id);
                            return(View(student));
                        }

                        //call of the verfication Size method
                        bool sizeIsCorrect = check.checkSize(upload.ContentLength);

                        if (sizeIsCorrect == false)
                        {
                            ViewBag.ErrorSize = ErrorMessages.ErrorSize();
                            Student student = db.Students.Include(s => s.Files).SingleOrDefault(s => s.ID == id);
                            return(View(student));
                        }

                        //Remove the the previous image
                        if (studentToUpdate.Files.Any(f => f.FileType == FileType.Avatar))
                        {
                            db.Files.Remove(studentToUpdate.Files.First(f => f.FileType == FileType.Avatar));
                        }

                        var avatar = new FileImage
                        {
                            FileName    = System.IO.Path.GetFileName(upload.FileName),
                            FileType    = FileType.Avatar,
                            ContentType = upload.ContentType
                        };

                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            avatar.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        studentToUpdate.Files = new List <FileImage> {
                            avatar
                        };
                    }

                    db.Entry(studentToUpdate).State = EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
                catch (RetryLimitExceededException /* dex */)
                {
                    //Log the error (uncomment dex variable name and add a line here to write a log.
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                }
            }
            return(View(studentToUpdate));
        }