Example #1
0
        public async Task <IActionResult> Create(createAdsModel advertisement)
        {
            if (ModelState.IsValid)
            {
                string uniquefilename = null;
                if (advertisement.Image != null && advertisement.Image.Length > 0)
                {
                    uniquefilename = ProccedFileUpload(advertisement);
                }

                Advertisement ads = new Advertisement();

                ads.UniqueID     = "#" + RandomString(6);
                ads.Title        = advertisement.Title;
                ads.BodyText     = advertisement.BodyText;
                ads.CreationDate = DateTime.UtcNow.AddHours(4);
                ads.ValidUntil   = advertisement.ValidUntil;
                ads.Image        = uniquefilename;

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

                return(RedirectToAction(nameof(Index)));
            }
            return(View(advertisement));
        }
Example #2
0
        private string ProccedFileUpload(createAdsModel model)
        {
            string uniquefilename = null;

            if (model.Image != null && model.Image.Length > 0)
            {
                var uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                uniquefilename = Guid.NewGuid().ToString() + "_" + model.Image.FileName;
                string filePath = Path.Combine(uploadFolder, uniquefilename);
                using (var filestream = new FileStream(filePath, FileMode.Create))
                {
                    model.Image.CopyTo(filestream);
                }
            }
            return(uniquefilename);
        }