I'm trying to validate that only .jpg file types can be uploaded into my local file storage and then include the URL of that location in the database.
I would like to check for the validation on each of the files that I'm trying to upload and if either file does NOT include .jpg then I would like to halt the application from moving forward with a message stating "Invalid file format".
I have the following code below. At this time I'm not getting any error messages, but rather the page will just refresh without displaying an error or message and it will also not proceed with the correct file format.
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert(DepartmentPageCreateVM departmentPageVM, List<IFormFile> files)
{
if (ModelState.IsValid)
{
if (departmentPageVM.DepartmentPage.DepartmentPageId == 0)
{
_context.DepartmentPages.Add(departmentPageVM.DepartmentPage);
}
else
{
_context.DepartmentPages.Update(departmentPageVM.DepartmentPage);
}
_context.SaveChanges();
string wwwRootPath = _webHostEnvironment.WebRootPath;
if (files != null)
{
foreach (IFormFile file in files)
{
//string productPath = @"images\departments\department-" + departmentPageVM.DepartmentPage.DepartmentPageId;
string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
string productPath = @"images/departments/department-" + departmentPageVM.DepartmentPage.DepartmentPageId;
string finalPath = Path.Combine(wwwRootPath, productPath);
if (!Directory.Exists(finalPath))
Directory.CreateDirectory(finalPath);
// check file extension
string extFile = Path.GetExtension(fileName);
if (extFile != ".jpg")
{
ModelState.AddModelError("", "Invalid file format");
}
using (var fileStream = new FileStream(Path.Combine(finalPath, fileName), FileMode.Create))
{
file.CopyTo(fileStream);
}
DepartmentPhoto departmentPhoto = new()
{
//DepartmentPhotoImageURL = @"\" + productPath + @"\" + fileName,
DepartmentPhotoImageURL = @"/" + productPath + @"/" + fileName,
DepartmentPageId = departmentPageVM.DepartmentPage.DepartmentPageId,
};
if (departmentPageVM.DepartmentPage.DepartmentPhoto == null)
departmentPageVM.DepartmentPage.DepartmentPhoto = new List<DepartmentPhoto>();
departmentPageVM.DepartmentPage.DepartmentPhoto.Add(departmentPhoto);
}
_context.DepartmentPages.Update(departmentPageVM.DepartmentPage);
_context.SaveChanges();
}
return RedirectToAction("Index");
}
else
{
departmentPageVM.DepartmentList = _context.Departments.ToList().Select(u => new SelectListItem
{
Text = u.DepartmentName,
Value = u.DepartmentId.ToString()
});
return View(departmentPageVM);
}
}
File Input in View
<div class="form-group mt-4">
<label asp-for="DepartmentPage.DepartmentPhoto"></label>
<input type="file" name="files" class="form-control" multiple />
</div>
ViewModel
public class DepartmentPageCreateVM
{
public DepartmentPage DepartmentPage { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> DepartmentList { get; set; }
}
DepartmentPhoto Model
public class DepartmentPhoto
{
public int DepartmentPhotoId { get; set; }
public string? DepartmentPhotoImageURL { get; set; }
public int DepartmentPageId { get; set; }
public DepartmentPage? DepartmentPage { get; set; }
}
DepartmentPage Model
public class DepartmentPage
{
public int DepartmentPageId { get; set; }
public string Name { get; set; }
public string ContentBody { get; set; }
public int DepartmentId { get; set; }
[ValidateNever]
public Department Department { get; set; }
[ValidateNever]
public List<DepartmentPhoto> DepartmentPhoto { get; set; }
}