这是我的更新,我在ImageUrl空检查中添加了一个调试点,甚至还有一个ImageUrl,它表示为null。
ublic IActionResult Upsert(ProductVM obj,IFormFile file)
{
if (ModelState.IsValid)
{
string wwwRootPath = _hostEnvironment.WebRootPath;
if (file != null)
{
string fileName = Guid.NewGuid().ToString();
var uploads = Path.Combine(wwwRootPath, @"Images\products");
var extention = Path.GetExtension(file.FileName);
if (obj.Product.ImageUrl != null)
{
var oldImagePath = Path.Combine(wwwRootPath,obj.Product.ImageUrl.TrimStart('\\'));
if (System.IO.File.Exists(oldImagePath))
{
System.IO.File.Delete(oldImagePath);
}
}
using (var fileStreams = new FileStream(Path.Combine(uploads, fileName + extention), FileMode.Create))
{
file.CopyTo(fileStreams);
}
obj.Product.ImageUrl = @"\Images\products\" + fileName + extention;
}
if (obj.Product.Id == 0)
{
_unitOfWork.Product.Add(obj.Product);
}
else
{
_unitOfWork.Product.Update(obj.Product);
}
_unitOfWork.Save();
TempData["success"] = "Produs adaugat cu succes!";
return RedirectToAction("Index");
}
return View(obj);
}发布于 2022-07-05 13:13:29
https://stackoverflow.com/questions/70722465
复制相似问题