Beispiel #1
0
        private string ProccedFileUpload(CreateModelBlog model)
        {
            string uniquefilename = null;

            if (model.Photo != null && model.Photo.Length > 0)
            {
                var uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                uniquefilename = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                string filePath = Path.Combine(uploadFolder, uniquefilename);
                using (var filestream = new FileStream(filePath, FileMode.Create))
                {
                    model.Photo.CopyTo(filestream);
                }
            }
            return(uniquefilename);
        }
Beispiel #2
0
        public async Task <IActionResult> Create(CreateModelBlog model)
        {
            if (ModelState.IsValid)
            {
                string uniquefilename = null;
                if (model.Photo != null && model.Photo.Length > 0)
                {
                    uniquefilename = ProccedFileUpload(model);
                }

                Blog blog = new Blog();
                blog.Title = model.Title;
                blog.Text  = model.Text;
                blog.Photo = uniquefilename;


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

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