2017-09-25 211 views
-1

我在部署應用程序時出現此錯誤。 找不到路徑'c:\ windows \ system32 \ inetsrv \〜\ App_Data \ xxxx.pdf'的一部分。部署.NET應用程序

異常詳細信息:System.IO.DirectoryNotFoundException:找不到路徑'c:\ windows \ system32 \ inetsrv \〜\ App_Data \ XXXX.pdf'的一部分。

該應用的默認方法是重定向到pdf文件。

這是我的代碼

FileStream fs = new FileStream("~/App_Data/xxx.pdf", FileMode.Create); 

     Document doc = new Document(PageSize.A4, 25, 25, 30, 30); 
     doc.SetMargins(40f, 40f, 40f, 20f); 
     PdfWriter writer = PdfWriter.GetInstance(doc, fs); 
     doc.Open(); 

PLZ任何幫助嗎?

感謝,

回答

1

FileStream你的路徑文件使用ASP.NET相對路徑與波浪號前綴,請嘗試使用Server.MapPath方法將其映射到正確的路徑:

FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/xxx.pdf"), FileMode.Create); 

如果動作方法內部存在的代碼在控制器類中,只要使用HttpContext.Current.Server.MapPath

FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/xxx.pdf"), FileMode.Create); 

類似的問題:

Read contents of a file using a relative path in a Web Application

ASP.NET C# - Save FileStream on server

+0

它給我的紅色下劃線服務器。和這個錯誤。非靜態字段,方法或屬性需要對象引用。 – Tuta

+0

嘗試使用完全限定的名稱:'System.Web.HttpContext.Current.Server.MapPath(「〜/ App_Data/xxx.pdf」)'或者如果它在控制器中:'HttpContext.Current.Server.MapPath(「〜 /App_Data/xxx.pdf「)'。 –