[英]Access denied when saving to file path
我有一個看起來像這樣的上傳方法
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:“拒絕訪問路徑 'C:\Users\chris\Source\Repos\inventory2.0\PIC_Program_1.0\Content\images\Components\498.jpg'。”
為什么會這樣,我該如何解決?
無論用戶正在執行該代碼,都無權寫入該文件路徑。 如果您從 go 到 C:\Users\chris\Source\Repos\inventory2.0\PIC_Program_1.0\Content\images\Components,右鍵單擊,屬性,安全選項卡,您將看到有權限的用戶以及這些權限是什么. 您可以在那里添加或編輯您的用戶權限。
我認為問題是您的應用程序用戶無權訪問您的文件夾。 如果您在 VS IIS express 中對此進行測試,那么您應該為當前用戶授予權限。
但是,如果您從 IIS 服務器收到此錯誤消息。 然后您應該授予應用程序池標識(IIS Apppool\apppoolname)的權限。
進程監視器可以幫助您始終修復拒絕訪問錯誤。 您只需要為 Result ="access is denied" 創建一個過濾器。 然后它會告訴你需要誰和什么許可。
https://docs.microsoft.com/en-us/sysinternals/downloads/procmon
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.