Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("Name,Url,Click,EndTime,ID,IsActive,IsDeleted,ModifiedOn,PhotoURL")] Advertisement advertisement)
        {
            if (ModelState.IsValid)
            {
                var pictures = Request.Form.Files;
                for (int i = 0; i < pictures.Count; i++)
                {
                    var picture = pictures[i];


                    var    fileName      = Guid.NewGuid() + Path.GetExtension(picture.FileName);
                    string uploadsFolder = Path.Combine(Environment.WebRootPath, "uploads");
                    string filePath      = Path.Combine(uploadsFolder, fileName);
                    advertisement.PhotoURL = fileName;
                    advertisement.Click    = 0;
                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        picture.CopyTo(fileStream);
                    }
                    _context.AddRange(advertisement);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(advertisement));
        }