예제 #1
0
        public async Task <IActionResult> Create([Bind("Upload,Photo,Id")] AboutChooseUs aboutChooseUs)
        {
            if (aboutChooseUs.Upload == null)
            {
                ModelState.AddModelError("Upload", "Şəkil məcburidir");
            }
            else
            {
                if (aboutChooseUs.Upload.ContentType != "image/jpeg" && aboutChooseUs.Upload.ContentType != "image/png" && aboutChooseUs.Upload.ContentType != "image/gif")
                {
                    ModelState.AddModelError("Upload", "Siz yalnız png,jpg və ya gif faylı yükləyə bilərsiniz");
                }

                if (aboutChooseUs.Upload.Length > 1048576)
                {
                    ModelState.AddModelError("Upload", "Fayl ölcüsu maximum 1MB ola bilər");
                }
            }
            if (ModelState.IsValid)
            {
                var fileName = _fileManager.Upload(aboutChooseUs.Upload);
                aboutChooseUs.Photo = fileName;

                _context.Add(aboutChooseUs);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(aboutChooseUs));
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Upload,Photo,Id")] AboutChooseUs aboutChooseUs)
        {
            if (id != aboutChooseUs.Id)
            {
                return(NotFound());
            }

            if (aboutChooseUs.Upload == null)
            {
                ModelState.AddModelError("Upload", "Şəkil məcburidir");
            }
            if (ModelState.IsValid)
            {
                try
                {
                    if (aboutChooseUs.Upload != null)
                    {
                        if (aboutChooseUs.Upload.ContentType != "image/jpeg" && aboutChooseUs.Upload.ContentType != "image/png" && aboutChooseUs.Upload.ContentType != "image/gif")
                        {
                            ModelState.AddModelError("Upload", "Siz yalnız png,jpg və ya gif faylı yükləyə bilərsiniz");
                            return(View(aboutChooseUs));
                        }

                        if (aboutChooseUs.Upload.Length > 1048576)
                        {
                            ModelState.AddModelError("Upload", "Fayl ölcüsu maximum 1MB ola bilər");
                            return(View(aboutChooseUs));
                        }

                        var oldFile = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", aboutChooseUs.Photo);
                        _fileManager.Delete(oldFile);

                        var fileName = _fileManager.Upload(aboutChooseUs.Upload, "wwwroot/uploads");
                        aboutChooseUs.Photo = fileName;
                    }

                    _context.Update(aboutChooseUs);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AboutChooseUsExsist(aboutChooseUs.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(aboutChooseUs));
        }