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文件夹,但我得到这个异常“系统.未经授权的异常:'访问路径'**\wwwroot\Images\Product'被拒绝。'“在我的windows 11机器上,我试图给所有所需的权限,该文件夹,但仍然得到它不能让我错过了什么
1条答案
按热度按时间zlwx9yxi1#
1.转到服务器文件夹
1.右键单击并选择属性
1.转到安全选项卡
1.按编辑
1.单击添加
1.键入IIS_IUSRS,然后单击检查名称
1.添加添加用户
另请阅读以下文件上传安全注意事项https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-8.0