public IActionResult Upsert(ProductVM productVM,IFormFile? file)
{
if (ModelState.IsValid)
{
string wwwwRootPath = _webHostEnvironment.WebRootPath;
if(file!=null)
{
string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
string productPath = Path.Combine(wwwwRootPath,@"Images\Product");
var filePath = Path.Combine(fileName, productPath);
using (var fileStream = new FileStream(filePath, FileMode.Create ))
{
file.CopyTo(fileStream);
}
productVM.Product.ImageUrl = @"\Images\Product\" + fileName;
}
_uniOfWork.Product.Add(productVM.Product);
_uniOfWork.Save();
TempData["success"] = "Category created successfully";
return RedirectToAction("Index");
}
else
{
productVM.CategoryList = _uniOfWork.Category
.GetAll()
.Select(u => new SelectListItem
{
Text = u.Name,
Value = u.Id.ToString(),
});
}
return View(productVM);
}
我试图在 wwwroot-> images 文件夹中保存文件,但收到此异常“System.UnauthorizedAccessException:'访问路径 '**\wwwroot\Images\Product' 被拒绝。' “在我的 Windows 11 机器上,我尝试授予该文件夹所有必需的权限,但仍然无法弄清楚我错过了什么
另请阅读以下文件上传安全注意事项 https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-8.0