コード例 #1
0
        public async Task <IActionResult> Create([Bind("Title,Upload,Photo,Id,Content,Description,Link,TextStyleLeft,TextStyleRight,Status")] FashionSlider fashion)
        {
            if (fashion.Upload == null)
            {
                ModelState.AddModelError("Upload", "The Photo field is required.");
            }
            else
            {
                if (fashion.Upload.ContentType != "image/jpeg" && fashion.Upload.ContentType != "image/png" && fashion.Upload.ContentType != "image/gif")
                {
                    ModelState.AddModelError("Upload", "You can only download png, jpg or gif file");
                }

                if (fashion.Upload.Length > 1048576)
                {
                    ModelState.AddModelError("Upload", "The file size can be a maximum of 1 MB");
                }
            }
            if (ModelState.IsValid)
            {
                var fileName = _fileManager.Upload(fashion.Upload);
                fashion.Photo = fileName;

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

                return(RedirectToAction(nameof(Index)));
            }
            return(View(fashion));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Title,Upload,Photo,Id,Content,Description,Link,TextStyleLeft,TextStyleRight,Status")] FashionSlider fashion)
        {
            if (id != fashion.Id)
            {
                return(NotFound());
            }

            if (fashion.Upload == null)
            {
                ModelState.AddModelError("Upload", "The Photo field is required.");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (fashion.Upload != null)
                    {
                        if (fashion.Upload.ContentType != "image/jpeg" && fashion.Upload.ContentType != "image/png" && fashion.Upload.ContentType != "image/gif")
                        {
                            ModelState.AddModelError("Upload", "You can only download png, jpg or gif file");
                            return(View(fashion));
                        }

                        if (fashion.Upload.Length > 1048576)
                        {
                            ModelState.AddModelError("Upload", "The file size can be a maximum of 1 MB");
                            return(View(fashion));
                        }

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

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

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