Click here to Skip to main content
14,358,122 members
Rate this:
Please Sign up or sign in to vote.
See more:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<iactionresult> Create([Bind("EmployeesId,EmployeeName,EmployeeCode,Joindate,Email,Phone,Address,DateOfBirth,PositionName,DepartmentId,companyId,Basic,Additional,Allowance,Workhours,CardNum,Status,Picture")] Employee employee, IFormFile file)
{
if (ModelState.IsValid)
{
_context.Add(employee);
await _context.SaveChangesAsync();
if (file != null || file.Length != 0)
{
// Create a File Info
FileInfo fi = new FileInfo(file.FileName);

// This code creates a unique file name to prevent duplications
// stored at the file location
var newFilename = employee.EmployeesId + "_" + String.Format("{0:d}",
(DateTime.Now.Ticks / 10) % 100000000) + fi.Extension;
var webPath = hostingEnvironment.WebRootPath;
var path = Path.Combine("", webPath + @"\img\" + newFilename);

// IMPORTANT: The pathToSave variable will be save on the column in the database
var pathToSave = @"/img/" + newFilename;

// This stream the physical file to the allocate wwwroot/ImageFiles folder
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}

// This save the path to the record
employee.Picture = pathToSave;
_context.Update(employee);
await _context.SaveChangesAsync();
}
return RedirectToAction(nameof(Index));
}
ViewData["companyId"] = new SelectList(_context.company, "companyId", "Name", employee.companyId);
ViewData["DepartmentId"] = new SelectList(_context.Departments, "DepartmentId", "DepartmenName", employee.DepartmentId);
return View(employee);
}

What I have tried:

how to upload multiple file in asp.net mvc core ( Create, Edit, Details, Delete )
Posted
Comments
Richard Deeming 4-Oct-19 13:47pm
   
Well, you've told us what you're trying to do, and shown us an (unformatted) block of code. But you seem to have forgotten to describe a problem, or ask a question.

Click the green "Improve question" link. Update your question with a clear description of the problem you're having. Include the full details of any error messages, and remember to indicate which line of code they relate to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100