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

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

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

                return(RedirectToAction(nameof(Index)));
            }
            return(View(homeCounter));
        }
示例#2
0
        public async Task <IActionResult> Edit(int id, [Bind("Count,Upload,Photo,Id,Content")] HomeCounter homeCounter)
        {
            if (id != homeCounter.Id)
            {
                return(NotFound());
            }

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

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

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

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

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