public async Task <IActionResult> CreateLaptop(ManagerProductViewModel viewModel) { viewModel.Laptop.SubDepartment = _DepartmentData.GetSubDepartmentById(viewModel.Laptop.SubDepartmentId); var usersfiles = HttpContext.Request.Form.Files; if (ModelState.IsValid) { _ProductData.AddNewProduct(viewModel.Laptop); foreach (var file in usersfiles) { // TO DO if without "upload" create one var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); //Make sure File is Jpg if (fileName.EndsWith(".jpg") || fileName.EndsWith(".png") || fileName.EndsWith(".gif")) { //Save Files To WWWRoot/UpLoads Folder var filePath = _env.ContentRootPath + "\\wwwroot\\" + fileName; await file.CopyToAsync(new FileStream(Path.Combine(Path.Combine(_env.WebRootPath, "uploads"), file.FileName + viewModel.Laptop.ProductId), FileMode.Create)); //Save Images to DataBase byte[] m_Bytes = ReadToEnd(file.OpenReadStream()); ProductImage oneImage = new ProductImage { Content = m_Bytes, FileName = fileName, FileType = FileType.Photo, ProductId = viewModel.Laptop.ProductId, ContentType = file.ContentType }; _ProductData.SaveProductImages(oneImage); } } return(RedirectToAction("Index")); } ManagerProductViewModel newviewModel = new ManagerProductViewModel(); newviewModel.SubDepartments = _DepartmentData.GetAllSubDepartments(); return(View(newviewModel)); }