我有一个看起来像这样的上传方法
public ActionResult UploadImage(int? id)
{
if (id == null)
return HttpNotFound();
Component c = db.Components.Find((int)id);
HttpPostedFileBase photo = Request.Files["image"];
if (photo != null && photo.ContentLength > 0)
{
var file = IGT.imagePath + "\\Components\\";
//bool exists = System.IO.Directory.Exists(Server.MapPath(file));
//if (!exists)
// System.IO.Directory.CreateDirectory(Server.MapPath(file));
var filename = file + id.ToString() + ".jpg";
if (!System.IO.Directory.Exists(file))
{
System.IO.Directory.CreateDirectory(file);
}
photo.SaveAs(filename);
c.Image_Url = IGT.baseUrl + "/Content/images/Components/" + id.ToString() +".jpg";
db.SaveChanges();
}
但是我在
photo.SaveAs(filename);
收到错误消息说System.UnauthorizedAccessException: 'Access to the path 'C:\Users\chris\Source\Repos\inventory2.0\PIC_Program_1.0\Content\images\Components\498.jpg' is denied.'
为什么会这样,我该如何解决?
最佳答案
无论执行该代码的用户是谁,都没有写该文件路径的权限。如果转到C:\ Users \ chris \ Source \ Repos \ inventory2.0 \ PIC_Program_1.0 \ Content \ images \ Components,右键单击,属性,“安全性”选项卡,您将看到具有权限的用户以及这些权限是什么。您可以在此处添加或编辑用户权限。
关于c# - 保存到文件路径时访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61195724/